Say I have a numpy array a = np.array([1, 5, 3, 2, 4, 6, 7]). Now I have another numpy array b = np.array([-1, -2, 3, 2, -1, -3]). The length of b is smaller than or equal to a. I wanna find the index i of the smallest element in a such that b[i] > 0. So in the example above, the result will be 3 since according to b only indices 2, 3 are valid and a[2] == 3 and a[3] == 2, so index 3 is chosen.
My current solution is
smallest = np.inf
index = None
for i in range(len(b)):
if b[i] > 0:
if(a[i] < smallest):
smallest = a[i]
index = i
I am not sure if I can use numpy to do it more efficiently. Any advice is appreciated. Thank you.