I want to add the values in a numpy array to the values in a specific row of a numpy matrix.
Given:
A = [[0, 0], [0, 0]]
b = [1, 1]
I want to add b to the values in the first row in A. The expected output is:
[[1, 1], [0, 0]]
I tried using the "+" operator, but got an error:
>>> 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)
What is the best way to do this?