0
A = np.arange(36).reshape(6,6)
B = A.reshape(-1,3,2,3)
C = B.transpose(0,2,1,3)

I have a matrix A, which looks like

[[ 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 27 28 29]
 [30 31 32 33 34 35]]

and have transformed it into C, which looks like

[[[[ 0  1  2]
   [ 6  7  8]
   [12 13 14]]

  [[ 3  4  5]
   [ 9 10 11]
   [15 16 17]]]


 [[[18 19 20]
   [24 25 26]
   [30 31 32]]

  [[21 22 23]
   [27 28 29]
   [33 34 35]]]]

How would I transform C back into A? I have tried following this guide https://stackoverflow.com/a/32034565/5131031, however, I was not successful.

PiccolMan
  • 4,854
  • 12
  • 35
  • 53

1 Answers1

0

You can transpose on the same axes and reshape back to the original:

import numpy as np

A = np.arange(36).reshape(6,6)
B = A.reshape(-1,3,2,3)
C = B.transpose(0,2,1,3)

C.transpose(0,2,1,3).reshape(6,6)

result:

array([[ 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, 27, 28, 29],
       [30, 31, 32, 33, 34, 35]])
Mark
  • 90,562
  • 7
  • 108
  • 148