0
我想將numpy陣列中的值添加到numpy矩陣的特定行中的值。將陣列的值添加到numpy矩陣的行中
考慮:
A = [[0, 0], [0, 0]]
b = [1, 1]
我想在A. B加入A值的第一行中的預期結果是:
[[1, 1], [0, 0]]
我使用 「+」 操作符嘗試過,但出現錯誤:
>>> import numpy
>>> a = numpy.zeros(shape=(2,2))
>>> a
array([[ 0., 0.],
[ 0., 0.]])
>>> b = numpy.ones(shape=(1,2))
>>> b
array([[ 1., 1.]])
>>> a[0, :] += b
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: non-broadcastable output operand with shape (2,) doesn't match the broadcast shape (1,2)
這樣做的最佳方法是什麼?