InquisitiveInquirer
InquisitiveInquirer

Reputation: 219

Python: Inserting a vector into a matrix

I have the following code:

import numpy as np
x = np.zeros([4,N])
x[:,0]= np.vstack([1000,0,0,50])

However, I get the following error:

ValueError: could not broadcast input array from shape (4,1) into shape (4)

I'm quite confused as to why this isn't working, any help would be much appreciated.

Upvotes: 1

Views: 1336

Answers (2)

Kent Sommer
Kent Sommer

Reputation: 358

This is due to the way slicing works in numpy as x[:,0] in your case expects an array not a vector.

The correct way to achieve what you want is:

import np as numpy
x = np.zeros([4,N])
x[:,0]= np.array([1000,0,0,50])

Upvotes: 1

Marat
Marat

Reputation: 15738

x[:,0]= np.array([1000,0,0,50]).T

Upvotes: 1

Related Questions