This might be a simple problem but I am stuck with this one and the solution I found is not efficient (I think).
Let's say I have two numpy arrays, one containing indices for each position and the second one the valid indices:
import numpy as np
x = np.array([0, 1, 2, 1, 3, 2])
indices = np.array([True, True, False, False])
I would like to get an boolean array indicating where in the 1st array the value is the same as the ones contained in the second array. My solution which as I mentioned is not efficient is this:
indices2 = np.where(indices)[0]
y1 = (x == indices2[0]) | (x == indices2[1])
y2 = np.zeros_like(x, dtype=bool)
for i in indices2:
y2 = (x == i) | y2
np.all(y2 == y1)
True
y1
array([ True, True, False, True, False, False])
So, is there a more efficient, more numpy-style way to achieve this (without the for loop for example)?
Edit:
corrected some bugs and replace the example with a smaller one as mentioned in comments.
xmuch smaller for demonstrating, like 5 values, so it's easy to understand. (2) why is this not justx1 == x2? (3) if the first array in your question isx, what is the second array? Is itindices? Because none of the values inxare inindicessince they have different types.indices[x]?xto indexindices?