FreeMat: PERMUTE Array Permutation Function

FreeMat

- Main Page
- Related Pages

PERMUTE Array Permutation Function

Section: Array Generation and Manipulations

# Usage

The `permute` function rearranges the contents of an array according to the specified permutation vector. The syntax for its use is

        y = permute(x,p)

where `p` is a permutation vector - i.e., a vector containing the integers `1...ndims(x)` each occuring exactly once. The resulting array `y` contains the same data as the array `x`, but ordered according to the permutation. This function is a generalization of the matrix transpose operation.

# Example

Here we use `permute` to transpose a simple matrix (note that permute also works for sparse matrices):

    --> A = [1,2;4,5]

    A =
     1 2
     4 5

    --> permute(A,[2,1])

    ans =
     1 4
     2 5

    --> A'

    ans =
     1 4
     2 5

Now we permute a larger n-dimensional array:

    --> A = randn(13,5,7,2);
    --> size(A)

    ans =
     13  5  7  2

    --> B = permute(A,[3,4,2,1]);
    --> size(B)

    ans =
      7  2  5 13