Python: Why can't I add a 3x1 array to one column of a 3x100 array?

Viewed 48

Variable a has the shape (3,1) and variable b has the shape (3,100). Now, I want to add variable a to just one column of variable b, meaning:

x[:,ii] = a + b[:,ii]

However, I get this message:

could not broadcast input array from shape (3,3) into shape (3,)

What am I missing?

1 Answers

You need to use numpy.ravel() Because a.shape is (3,1) and you need (3,).

x[:,ii] = a.ravel() + b[:,ii]
Related