Finding index by iterating over each row of matrix
问题 I have an numpy array 'A' of size 5000x10 . I also have another number 'Num' . I want to apply the following to each row of A: import numpy as np np.max(np.where(Num > A[0,:])) Is there a pythonic way than writing a for loop for above. 回答1: You could use argmax - A.shape[1] - 1 - (Num > A)[:,::-1].argmax(1) Alternatively with cumsum and argmax - (Num > A).cumsum(1).argmax(1) Explanation : With np.max(np.where(..) , we are basically looking to get the last occurrence of matches along each row