emmagras
emmagras

Reputation: 1407

Inserting one matrix as an element of another matrix using Numpy

I'm using Numpy and have a 7x12x12 matrix whose values I would like to populate in 12x12 chunks, 7 different times. Suppose I have these 12x12 matrices:

first_Matrix
second_Matrix
third_Matrix
... (etc)
seventh_Matrix = first_Matrix + second_Matrix + third_Matrix...

that I'd like to add to:

grand_Matrix

How can I do this? I assume there is a better way than loops that map the coordinates from one matrix to the next, and if there's not, could someone please write out the code for mapping first_Matrix into the first 12x12 element of grand_Matrix?

Upvotes: 0

Views: 403

Answers (1)

eumiro
eumiro

Reputation: 212835

grand_Matrix[0,...] = first_Matrix
grand_Matrix[1,...] = second_Matrix

and so on.

Anyway, as @Lattyware commented, it is a bad design to have extra names for so many such homogenous objects.

If you have a list of 12x12 matrices:

grand_Matrix = np.vstack(m[None,...] for m in matrices)

None adds a new dimension to each matrix and stacks them along this dimension.

Upvotes: 3

Related Questions