[英]How to add a array to a column of a matrix? (python numpy)
Like this:像这样:
import numpy as np
a = np.zeros((3,3))
b = np.ones((3,1))
a[:,2] += b
expected:预期的:
a =
0,0,1
0,0,1
0,0,1
in fact:实际上:
ValueError: non-broadcastable output operand with shape (3,) doesn't match the broadcast shape (3,3)
What should I do?我应该怎么办?
Specifying the range of column is required需要指定列的范围
eg a[:,0:1]
for column 0, a[:,1:2]
for column 1, and a[:,2:]
for column 2.例如a[:,0:1]
表示第 0 列, a[:,1:2]
表示第 1 列, a[:,2:]
表示第 2 列。
import numpy as np
a = np.zeros((3,3))
b = np.ones((3,1))
a[:,2:] += b
output: output:
array([[0., 0., 1.], [0., 0., 1.], [0., 0., 1.]])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.