Given the following matrix,
In [0]: a = np.array([[1,2,9,4,2,5],[4,5,1,4,2,4],[2,3,6,7,8,9],[5,6,7,4,3,6]])
Out[0]:
array([[1, 2, 9, 4, 2, 5],
[4, 5, 1, 4, 2, 4],
[2, 3, 6, 7, 8, 9],
[5, 6, 7, 4, 3, 6]])
I want to get the indices of the rows that have 9 as a member. This is,
idx = [0,2]
Currently I am doing this,
def myf(x):
if any(x==9):
return True
else:
return False
aux = np.apply_along_axis(myf, axis=1, arr=a)
idx = np.where(aux)[0]
And I get the result I wanted.
In [1]: idx
Out[1]: array([0, 2], dtype=int64)
But this method is very slow (meaning maybe there is a faster way) and certainly not very pythonic.
How can I do this in a cleaner, more pythonic but mainly more efficient way?
Note that this question is close to this one but here I want to apply the condition on the entire row.