Home > Software engineering >  Numpy: How to unwrap of a matrix
Numpy: How to unwrap of a matrix

Time:11-26

I am hoping to reshape matrices in such a form

A = 
[[1,2,3],
[4,5,6],
[7,8,9]]

B = 
[[10,11,12],
[13,14,15],
[16,17,18]]

Z = [[1, 2, 3 10, 11, 12],
[4, 5, 6, 13, 14, 15],
[7,8,9, 16, 17 ,18]] 

Where A,B are a 3x3 matrices but z is a 3x6 matrix. I'd like to be able to apply it to higher dimensions. np.ravel returns a flattened array so I can't use that because the output matrix will then be

Z = 
[[1 ,2 ,3, 4, 5, 6],
[7, 8, 9, 10 ,11, 12],
[13, 14, 15, 16, 17, 18]]

I can't use np.reshape to (6,6) either because it would flatten the array before, since it results in the same matrix as above. Looking for some way to implement this.

CodePudding user response:

Assume A and B are always the same shape:

np.vstack((A, B)).reshape(len(A), -1)

#array([[ 1,  2,  3,  4,  5,  6],
#       [ 7,  8,  9, 10, 11, 12],
#       [13, 14, 15, 16, 17, 18]])
  • Related