I am new in Python and my question might be too obvious, but I did not find a sufficiently good answer.
Suppose I have a 2D array a=np.array([[1,2,3],[4,5,6],[7,8,9]])
. How can I subscribe those elements that satisfy a condition? Suppose, I want to increase by one those elements of a
that are greater than 3. In Matlab, I would do it in 1 line:
a(a>3)=a(a>3)+1.
What about Python?
The result should be [[1,2,3],[5,6,7],[8,9,10]]
.
I am aware that there are functions that can return the indeces I need, like np.where
. I am also aware that there is a way of indexing 2D array with two 1D arrays. I was not able to combine those together.
Of course, I am able to do it with for loop. I am interested, is there a convenient Matlab-like way of doing this?
Thanks