Search code examples
arraysmatlabmultidimensional-arrayreshapepermute

Alternative shorter method to permute and reshape a multidimensional-array


I have the following multidimensional array:

a(:,:,1) =
    11    12    13
    21    22    23

a(:,:,2) =
    21    22    23
    31    32    33

a(:,:,3) =
    31    32    33
    41    42    43

The end result looks as follows:

e =
    11    21    31
    12    22    32
    13    23    33
    21    31    41
    22    32    42
    23    33    43

In order to obtain array e, I did the following:

b=permute(a,[2,1,3])  
c=reshape(b,1,6,[])  
d=permute(c,[2,1,3])

Is there a cleaner or more elegant way of achieving the same result?


Solution

  • Here permute used once:

    b = permute(a,[2 1 3]);
    result = reshape(b, [], 3)