<div dir="ltr"><div>I'm not 100% sure that I get the question, but does this help at all?</div><div> </div><div>>>> a = numpy.array([3,2,8,7])<br>>>> b = numpy.array([1,3,2,4,5,7,6,8,9])<br>>>> c = set(a) & set(b)<br>>>> c #contains elements of a that are in b (and vice versa)<br>set([8, 2, 3, 7])<br>>>> indices = numpy.where([x in c for x in b])[0]<br>>>> indices #indices of b where the elements of a in b occur<br>array([1, 2, 5, 7], dtype=int64)</div><div> </div><div>-Mark</div><div> </div></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Dec 30, 2015 at 6:45 AM, Nicolas P. Rougier <span dir="ltr"><<a href="mailto:Nicolas.Rougier@inria.fr" target="_blank">Nicolas.Rougier@inria.fr</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
I’m scratching my head around a small problem but I can’t find a vectorized solution.<br>
I have 2 arrays A and B and I would like to get the indices (relative to B) of elements of A that are in B:<br>
<br>
>>> A = np.array([2,0,1,4])<br>
>>> B = np.array([1,2,0])<br>
>>> print (some_function(A,B))<br>
[1,2,0]<br>
<br>
# A[0] == 2 is in B and 2 == B[1] -> 1<br>
# A[1] == 0 is in B and 0 == B[2] -> 2<br>
# A[2] == 1 is in B and 1 == B[0] -> 0<br>
<br>
Any idea ? I tried numpy.in1d with no luck.<br>
<br>
<br>
Nicolas<br>
<br>
_______________________________________________<br>
NumPy-Discussion mailing list<br>
<a href="mailto:NumPy-Discussion@scipy.org">NumPy-Discussion@scipy.org</a><br>
<a href="https://mail.scipy.org/mailman/listinfo/numpy-discussion" target="_blank" rel="noreferrer">https://mail.scipy.org/mailman/listinfo/numpy-discussion</a><br>
</blockquote></div><br></div>