简体   繁体   中英

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.

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

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])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM