0

if I have a 2D matrix, and I want to assign a vector [1,1,1] into each cell of my M matrix

vector = np.array([1,1,1])
M= np.zeros((4,4)).astype(np.object)
M[:]=vector.astype(object)

This will obviously give me the error that:

ValueError: could not broadcast input array from shape (2) into shape (3,3)

So is there any method I can store my 3d vector into each cell of my 4x4 M matrix?

Thanks!

I know that if I iterate the ndarray I can do it

for i in range(np.shape(M)[0]):
    for j in range(np.shape(M)[1]):
        M[i][j]=vector

just wandering whether there's a simple syntax for this

1 Answers1

1

You need to declare what the entries of your matrix should contain with the argument dtype, namely vector.dtype.

This link might help: Numpy - create matrix with rows of vector

corvelk
  • 171
  • 7