Reputation: 177
I am trying to mask some elements in an array such that a mathematical operation is not applied to these elements.
I ran this code
import numpy as np
inp = np.random.randn(5, 5)
c = np.random.randn(5,5)
mask = inp > 0
inp[mask] += c
print(inp)
but I got this error
ValueError: operands could not be broadcast together with shapes (25,) (5,5) (25,)
Upvotes: 0
Views: 113
Reputation: 4449
inp += mask.astype(int) * c
# -- or simplified to:
inp += mask * c
Upvotes: 1