[Numpy-discussion] searchsorted for exact matches, not preserving order
Robert Kern
robert.kern at gmail.com
Fri Sep 11 16:52:42 EDT 2009
On Fri, Sep 11, 2009 at 15:46, Andrew Jaffe<a.h.jaffe at gmail.com> wrote:
> On 11/09/2009 08:33, Robert Kern wrote:
>> On Fri, Sep 11, 2009 at 10:24, Andrew Jaffe<a.h.jaffe at gmail.com> wrote:
>>> Dear all,
>>>
>>> I've got two (integer) arrays, and I want to find the indices in the
>>> first one that have entries in the second. I.E. I want all idx s.t.
>>> there exists a j with a[idx]=b[j]. Here is my current implementation
>>> (with a = pixnums, b=surveypix)
>>
>> numpy.setmember1d() [or numpy.in1d() for the SVN trunk of numpy].
>>
> Robert,
>
> Thanks. But in fact this fails for my (possibly corner or edge) case:
> when the first array has duplicates that, in fact, are not in the second
> array, indices corresponding to those entries get returned. In general,
> duplicates are not necessarily treated right by this algorithm, I don't
> think.
>
> I can understand that this may be a feature, not a bug, but in fact for
> my use-case I want the algorithm to return the indices corresponding to
> all entries in ar1 with the same value, if that value appears anywhere
> in ar2.
That is the main feature that in1d() added over setmember1d(). If you
cannot use the SVN trunk of numpy, you can just copy the
implementation. It is not large.
In [4]: a = np.arange(5)
In [5]: b = np.hstack([a,a])
In [6]: c = np.arange(0, 10, 2)
In [7]: c
Out[7]: array([0, 2, 4, 6, 8])
In [8]: np.in1d(b, c)
Out[8]: array([ True, False, True, False, True, True, False, True,
False, True], dtype=bool)
--
Robert Kern
"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
-- Umberto Eco
More information about the NumPy-Discussion
mailing list