Main Content

permute

Permute array dimensions

Description

B = permute(A,dimorder) rearranges the dimensions of an array in the order specified by the vector dimorder. For example, permute(A,[2 1]) switches the row and column dimensions of a matrix A. In general, the ith dimension of the output array is the dimension dimorder(i) from the input array.

example

Examples

collapse all

Create a 3-by-4-by-2 array and permute it so that the first and third dimensions are switched, resulting in a 2-by-4-by-3 array.

rng default
A = rand(3,4,2)
A = 
A(:,:,1) =

    0.8147    0.9134    0.2785    0.9649
    0.9058    0.6324    0.5469    0.1576
    0.1270    0.0975    0.9575    0.9706


A(:,:,2) =

    0.9572    0.1419    0.7922    0.0357
    0.4854    0.4218    0.9595    0.8491
    0.8003    0.9157    0.6557    0.9340

B = permute(A,[3 2 1])
B = 
B(:,:,1) =

    0.8147    0.9134    0.2785    0.9649
    0.9572    0.1419    0.7922    0.0357


B(:,:,2) =

    0.9058    0.6324    0.5469    0.1576
    0.4854    0.4218    0.9595    0.8491


B(:,:,3) =

    0.1270    0.0975    0.9575    0.9706
    0.8003    0.9157    0.6557    0.9340

szB = size(B)
szB = 1×3

     2     4     3

Input Arguments

collapse all

Input array, specified as a vector, matrix, or multidimensional array.

Dimension order, specified as a row vector with unique, positive integer elements that represent the dimensions of the input array.

Extended Capabilities

Version History

Introduced before R2006a

expand all