Creating Matrix(array) where each element itself is a matrix

Clash Royale CLAN TAG#URR8PPP
Creating Matrix(array) where each element itself is a matrix
I have four mxn matrices. From those matrices I want to create a mxn matrix of matrices such that each element of new matrix would be a 2x2 matrix taking respective elements from each matrix. So far I can think of stacking all four matrices and then reshape each element along z axis and replace it. I believe there is more elegant solution to this.
Sample input:
$$
beginbmatrix
A_11 & A_12\
A_21 & A_22
endbmatrix
,
beginbmatrix
B_11 & B_12\
B_21 & B_22
endbmatrix
,
beginbmatrix
C_11 & C_12\
C_21 & C_22
endbmatrix
,
beginbmatrix
D_11 & D_12\
D_21 & D_22
endbmatrix
$$
Expected output:
$$
beginbmatrix
beginbmatrix
A_11 & B_11\
C_11 & D_11
endbmatrix & beginbmatrix
A_12 & B_12\
C_12 & D_12
endbmatrix\
beginbmatrix
A_21 & B_21\
C_21 & D_21
endbmatrix & beginbmatrix
A_22 & B_12\
C_22 & D_22
endbmatrix
end{bmatrix
$$
All are Numpy array
– Abhisek Maiti
Aug 9 at 19:39
It would really help for you to show some sample input matrices, as well as a desired output matrix.
– rahlf23
Aug 9 at 19:41
Please provide some sample. It it unclear of what elements of your mxn matrix the 2x2 matrix schould be constructed.
– WurzelseppQX
Aug 9 at 19:45
2 Answers
2
A sample creating function:
In [510]: def foo(astr,m,n):
...: alist = [astr+'%d%d'%(i,j) for i in range(m) for j in range(n)]
...: return np.array(alist).reshape(m,n)
In [511]: foo('A',2,2)
Out[511]:
array([['A00', 'A01'],
['A10', 'A11']], dtype='<U3')
A list of 4 such arrays:
In [512]: alist = [foo('A',2,2),foo('B',2,2),foo('C',2,2),foo('D',2,2)]
Various ways of stacking:
In [513]: np.stack(alist)
Out[513]:
array([[['A00', 'A01'],
['A10', 'A11']],
[['B00', 'B01'],
['B10', 'B11']],
[['C00', 'C01'],
['C10', 'C11']],
[['D00', 'D01'],
['D10', 'D11']]], dtype='<U3')
In [514]: np.stack(alist,2)
Out[514]:
array([[['A00', 'B00', 'C00', 'D00'],
['A01', 'B01', 'C01', 'D01']],
[['A10', 'B10', 'C10', 'D10'],
['A11', 'B11', 'C11', 'D11']]], dtype='<U3')
In [515]: _.shape
Out[515]: (2, 2, 4)
This can be reshaped in various ways:
In [516]: __.reshape(2,2,2,2)
Out[516]:
array([[[['A00', 'B00'],
['C00', 'D00']],
[['A01', 'B01'],
['C01', 'D01']]],
[[['A10', 'B10'],
['C10', 'D10']],
[['A11', 'B11'],
['C11', 'D11']]]], dtype='<U3')
In [517]: _.reshape(4,2,2)
Out[517]:
array([[['A00', 'B00'],
['C00', 'D00']],
[['A01', 'B01'],
['C01', 'D01']],
[['A10', 'B10'],
['C10', 'D10']],
[['A11', 'B11'],
['C11', 'D11']]], dtype='<U3')
Instead different axis, you can create one and transpose the axes to your heart's content.
Since all have mxn elements, make mxn iterations and at each iteration, build a 2x2 matrix from each element of the 4 matrices and then add the list to your final mxn matrix.
matrix1 = [[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]]
matrix2 = [[2, 2, 2, 2], [2, 2, 2, 2], [2, 2, 2, 2]]
matrix3 = [[3, 3, 3, 3], [3, 3, 3, 3], [3, 3, 3, 3]]
matrix4 = [[4, 4, 4, 4], [4, 4, 4, 4], [4, 4, 4, 4]]
matrixFinal =
for i in range(len(matrix1)):
matrixFinal.append()
for j in range(len(matrix1[i])):
matrixFinal[i].append ([[matrix1[i][j], matrix2[i][j]], [matrix3[i][j], matrix4[i][j]]])
print(matrixFinal)
I could do that but I think it would take time to iterate over all elements. I was expecting a more elegant solution if there is one. Anyway thanks
– Abhisek Maiti
Aug 9 at 20:03
You need to know every element in every matrix, you must do 4xmxn iterations but other than that I just gave my idea. If you do not think this answers your question, I can delete it.
– Notrius
Aug 9 at 20:09
No it is fine. I was just wondering if there is any other efficient way to go about it.
– Abhisek Maiti
Aug 9 at 20:13
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Are these just lists of lists, or are you using some framework on top of Python, like NumPy or Pandas?
– Patrick Haugh
Aug 9 at 19:37