Python numpy broadcasting when adding slices of 2d arrays

Viewed 28

I have two 2D numpy arrays. I want to add a slice of the array via broadcasting:

a = np.zeros((3,5))
b = np.zeros((3,5))

c = a + b[0,:]  # Works fine
c = a + b[:,0]  # -> ValueError: operands could not be broadcast together with shapes (3,5) (3,) 

The problem is, that b[:,0] creates a 1D array which is not properly broadcasted to a.

Is there an easy way to add 2D and 1D arrays?

0 Answers
Related