How to rearrange a 3d numpy array?

Viewed 90

I have a numpy ndarray, something like this (my array is much bigger, I am only giving this in order to explain what I need to do):

a = [[[ 0  1  2]
      [ 3  4  5]
      [ 6  7  8]]

     [[ 9 10 11]
      [12 13 14]
      [15 16 17]]

     [[18 19 20]
      [21 22 23]
      [24 25 26]]]

I am looking for an elegant way to rearrange the array in order to get something like this:

a_new=[[[ 0   9  18]
        [ 1  10  19]
        [ 2  11  20]]

       [[ 3 12 21]
        [ 4 13 22]
        [ 5 14 23]]

       [[ 6 15 24]
        [ 7 16 25]
        [ 8 17 26]]]
1 Answers

Simply permute axes -

a.transpose(1,2,0)
# or np.moveaxis(a,0,2)
Related