I have an array in numpy:
[1 2 3]
[4 5 6]
[7 8 9]
[10 11 12]
I would like to add 100 to all values that are greater than or equal to 3 and less than or equal to 8. How can I do this?
I have an array in numpy:
[1 2 3]
[4 5 6]
[7 8 9]
[10 11 12]
I would like to add 100 to all values that are greater than or equal to 3 and less than or equal to 8. How can I do this?
You could do something like this:
>>> import numpy as np
>>> x = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]])
>>> (x >= 3) * (x <= 8) * 100
array([[ 0, 0, 100],
[100, 100, 100],
[100, 100, 0],
[ 0, 0, 0]])
and then add that to your array. Note that (x >= 3) * (x <= 8) contains boolean values that are cast to 0 or 1 once you multiply them with the integer 100.