4

I would like to reshape an 2D numpy array in a 3D array consisting of 2x2 (or in general PxP) blocks, respecting the spatial positions.

x = np.arange(100).reshape(10,10)
x
array([[ 0,  1,  2, ...,  7,  8,  9],
       [10, 11, 12, ..., 17, 18, 19],
       [20, 21, 22, ..., 27, 28, 29],
       ..., 
       [70, 71, 72, ..., 77, 78, 79],
       [80, 81, 82, ..., 87, 88, 89],
       [90, 91, 92, ..., 97, 98, 99]])

y = some_reshape_and_transpose(x)
y[0]
array([[ 0,  1],
       [10, 11]])
y[1]
array([[ 2,  3],
       [12, 13]])
y.shape
(25, 2, 2)

The precise order of the blocks along the first dimension of the 3D tensor is not important. The reverse command would be appreciated as well !

Toool
  • 361
  • 3
  • 18

0 Answers0