user14492868
user14492868

Reputation: 177

masking elements in an array using numpy

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

Answers (1)

Woodford
Woodford

Reputation: 4449

inp += mask.astype(int) * c

# -- or simplified to:
inp += mask * c

Upvotes: 1

Related Questions