I have a NumPy array, A. I want to know the indexes of the elements in A equal to a value and which indexes satisfy some condition:
import numpy as np
A = np.array([1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4])
value = 2
ind = np.array([0, 1, 5, 10]) # Index belongs to ind
Here is what I did:
B = np.where(A==value)[0] # Gives the indexes in A for which value = 2
print(B)
[1 5 9]
mask = np.in1d(B, ind) # Gives the index values that belong to the ind array
print(mask)
array([ True, True, False], dtype=bool)
print B[mask] # Is the solution
[1 5]
The solution works, but I find it complicated. Also, in1d does a sort which is slow. Is there a better way of achieving this?