0

Imagine we have an array with 100 elements. we want to turn it into a 2x2 matrix which every sub-matrix is a 5x5 matrix itself. I've write it to this level:

import numpy as np

M = np.linspace(1,100,100)

MUL = M[0:25].reshape([5,5])
MUR = M[25:50].reshape([5,5])
MLL = M[50:75].reshape([5,5])
MLR = M[75:101].reshape([5,5])

Now I have my 5x5 sub-matrix, how can I turn them into a 2x2 matrix?

Thanks in advance ^^

Moty.R
  • 47
  • 1
  • 9
  • 1
    Reshape to (2,2,5,5) works, but you may need to transpose some axes to get the element order right. The general approach is to reshape into small enough blocks, transpose to reorder, and if need reshape back to a final shape. – hpaulj Jul 30 '20 at 17:32

1 Answers1

0

This works for this specific case.

M.reshape(2,2,5,5)

If you want more control over the order of the data you could build a new array manually.

A = np.zeros((2,2,5,5))
A[0,0,:,:] = MUL
A[0,1,:,:] = MUR
A[1,0,:,:] = MLL
A[1,1,:,:] = MLR
mpw2
  • 148
  • 8