[Numpy-discussion] searchsorted for exact matches, not preserving order
Fri Sep 11 15:52:42 CDT 2009
On Fri, Sep 11, 2009 at 15:46, Andrew Jaffe<firstname.lastname@example.org> wrote:
> On 11/09/2009 08:33, Robert Kern wrote:
>> On Fri, Sep 11, 2009 at 10:24, Andrew Jaffe<email@example.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].
> 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
> 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 : a = np.arange(5)
In : b = np.hstack([a,a])
In : c = np.arange(0, 10, 2)
In : c
Out: array([0, 2, 4, 6, 8])
In : np.in1d(b, c)
Out: array([ True, False, True, False, True, True, False, True,
False, True], dtype=bool)
"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