Home > Back-end >  How to add a 4X4 matrix values into a 6x6 matrix using numpy
How to add a 4X4 matrix values into a 6x6 matrix using numpy

Time:12-22

suppose i have multiple 4x4 matrices which i want to add to a final 6x6 zero matrix by adding some of the values in the designated coordination. how would i do this. I throughout of adding slices to np.zero 6x6 matrix , but i believe this may be quite tedious.

matrix 1 would go to this position first position and you have matrix 2 going to this position position 2. these two positions would be added and form the following final matrix Final position matrix

CodePudding user response:

Adding two arrays of differents dimensions is a little bit tricky with numpy.

However, with array comprehension, you could do it with the following "rustic" method :

Supposing M1 and M2 your 2 input arrays, M3 (from M1) and M4 (from M2) your temporary arrays and M5 the final array :

#Initalisation
M1 = np.array([[ 0.36,  0.48,  -0.36,  -0.48], [ 0.48,  0.64,  -0.48,  -0.64], [ -0.36, -0.48, 0.36, 0.48], [-0.48, -0.64, 0.48, 0.64]])
M2 = np.array([[ 0,  0,  0,  0], [ 0,  1.25,  0,  -1.25], [ 0,  0,  0,  0], [ 0,  -1.25,  0,  1.25]])
M3, M4 = np.zeros((6, 6)), np.zeros((6, 6))

#M3 and M4 operations
M3[0:4, 0:4] = M1[0:4, 0:4]   M3[0:4, 0:4]

M4[0:2, 0:2] = M2[0:2, 0:2]
M4[0:2, 4:6] = M2[0:2, 2:4]
M4[4:6, 0:2] = M2[2:4, 0:2]
M4[4:6, 4:6] = M2[2:4, 2:4]

#Final operation
M5 = M3 M4

print(M5)

Output :

[[ 0.36  0.48 -0.36 -0.48  0.    0.  ]
 [ 0.48  1.89 -0.48 -0.64  0.   -1.25]
 [-0.36 -0.48  0.36  0.48  0.    0.  ]
 [-0.48 -0.64  0.48  0.64  0.    0.  ]
 [ 0.    0.    0.    0.    0.    0.  ]
 [ 0.   -1.25  0.    0.    0.    1.25]]

Have a good day.

CodePudding user response:

You will need to encode some way of where your 4x4 matrices end up in the final 6x6 matrix. Suppose you have N (=2 in your case) such 4x4 matrices. You can then define two new arrays (shape Nx4) that denote the row and col indices of the final 6x6 matrix that you want your 4x4 matrices to end up in. Finally, you use fancy indexing and broadcasting to build up a Nx6x6 array which you can sum over. Your example:

import numpy as np

N = 2
arr = np.array([[
    [0.36, 0.48, -0.36, -0.48],
    [0.48, 0.64, -0.48, -0.64],
    [-0.36, -0.48, 0.36, 0.48],
    [-0.48, -0.64, 0.48, 0.64],
], [
    [0, 0, 0, 0],
    [0, 1.25, 0, -1.25],
    [0, 0, 0, 0],
    [0, -1.25, 0, 1.25],
]])
rows = np.array([
    [0, 1, 2, 3],
    [0, 1, 4, 5]
])
cols = np.array([
    [0, 1, 2, 3],
    [0, 1, 4, 5]
])
i = np.arange(N)

out = np.zeros((N, 6, 6))
out[
    i[:, None, None],
    rows[:, :, None],
    cols[:, None, :]
] = arr
out = out.sum(axis=0)

Gives as output:

array([[ 0.36,  0.48, -0.36, -0.48,  0.  ,  0.  ],
       [ 0.48,  1.89, -0.48, -0.64,  0.  , -1.25],
       [-0.36, -0.48,  0.36,  0.48,  0.  ,  0.  ],
       [-0.48, -0.64,  0.48,  0.64,  0.  ,  0.  ],
       [ 0.  ,  0.  ,  0.  ,  0.  ,  0.  ,  0.  ],
       [ 0.  , -1.25,  0.  ,  0.  ,  0.  ,  1.25]])

If you want even more control over where each row/col ends up, you can pull off some more trickery as follows:

rows = np.array([
    [1, 2, 3, 4, 0, 0],
    [1, 2, 0, 0, 3, 4]
])
cols = np.array([
    [1, 2, 3, 4, 0, 0],
    [1, 2, 0, 0, 3, 4]
])
i = np.arange(N)

out = np.pad(arr, ((0, 0), (1, 0), (1, 0)))[
    i[:, None, None],
    rows[:, :, None],
    cols[:, None, :]
].sum(axis=0)

which has the same output. This would allow you to shuffle the rows/cols of arr by shuffling the values 1-4 in the rows, cols arrays. I would prefer option 1 though.

  • Related