I have a 2D array like:
r1= np.array([[1,2,3,4],[2,3,4,5],[3,4,5,6]])
I need to find , for each row, the first occurence of a value greater than a default value. I am using this:
default=2
ans= np.argmax(r1>default,1)
The issue is that it works if there is a value greater than default but returns zero if such a value is not found. So it returns zero in 2 cases- 1) the first value in each row is greater than the default 2) no value in each is greater than default
#simple case:
In[31]: np.argmax(r1>2,1)
Out[31]: array([2, 1, 0], dtype=int64)
#trouble case- both returning zeros
In[32]: np.argmax(r1>7,1)
Out[32]: array([0, 0, 0], dtype=int64)
In[33]: np.argmax(r1>0.5,1)
Out[33]: array([0, 0, 0], dtype=int64)
I am currently using this to resolve this:
In[37]: np.any(r1>7,1) + np.argmax(r1>7,1)-1
Out[37]: array([-1, -1, -1], dtype=int64)
In[38]: np.any(r1>0.5,1) + np.argmax(r1>0.5,1)-1
Out[38]: array([0, 0, 0], dtype=int64)
Any other suggestions to simplify this?