I have a NumPy 2D array:
a = np.array([[1, 2], [3, 4], [5, 6], [7, 8]])
where each column of this array (i.e.: [1,3, 5, 7]
, and [2, 4, 6, 8]
) need to be transformed to a matrix of given size M1xM2
(using order='F' when reshaping). In this case, M1 = M2 = 2
. So, my desired output would be:
b = np.array([[[1, 5], [3, 7]], [[2, 6], [4, 8]]]).
I can easily achieve this by iterating over the columns. However, the number of columns can be any and the initial 2D array can be of up to 8 dimensions. How can I easily extend this solution to be used for more dimensions?
I suspect this is a common procedure and there is a built-in function to solve it, but haven't been able to find it.