Wednesday, 8 December 2021

Reshaping 3D numpy array for MATLAB requirement compatibility

Given an array of shape 3,2,2

np.array([[[1,2],[3,4]],[[5,6],[7,8]],[[9,10],[11,12]]])

I would like to reshape it into shape 2,2,3.

Downstream, this reshape data will be processed in MATLAB, and for some reason, the MATLAB-Package require the array to be shaped 2,2,3.

In MATLAB, the expected output should be

val(:,:,1) =

     1     2
     3     4


val(:,:,2) =

     5     6
     7     8


val(:,:,3) =

     9    10
    11    12

Simply reshaping arr.reshape(2,2,3) does not produced what I intend it to be.

Update: @hpaulj proposal produced

val(:,:,1) =

   1   3
   2   4


val(:,:,2) =

   5   7
   6   8


val(:,:,3) =

    9   11
   10   12


from Reshaping 3D numpy array for MATLAB requirement compatibility

No comments:

Post a Comment