[Numpy-discussion] intersect1d and setmember1d

Robert Cimrman cimrman3@ntc.zcu...
Mon Mar 2 02:56:35 CST 2009


Neil wrote:
> mudit sharma <mudit_19a <at> yahoo.com> writes:
> 
>> intersect1d and setmember1d doesn't give expected results in case there are
> duplicate values in either
>> array becuase it works by sorting data and substracting previous value. Is
> there an alternative in numpy
>> to get indices of intersected values.
>>
>> In [31]: p nonzero(setmember1d(v1.Id, v2.Id))[0]
>> [ 0  1  2  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25  
>                      <-------------- index 2 shouldn't be here look at the
>> data below.
>>  26 27 28 29]
>>
>> In [32]: p v1.Id[:10]
>> [ 232.  232.  233.  233.  234.  234.  235.  235.  237.  237.]
>>
>> In [33]: p v2.Id[:10]
>> [ 232.  232.  234.  234.  235.  235.  236.  236.  237.  237.]
>>
> 
> 
> As far as I know there isn't an obvious way to get the functionality of
> setmember1d working on non-unique inputs. However, I've needed this operation
> quite a lot, so here's a function I wrote that does it. It's only a few times
> slower than numpy's setmember1d. You're welcome to use it.

Hi Neil!

I would like to add your function to arraysetops.py - is it ok? Just the 
name would be changed to setmember1d_nu, to follow the naming in the 
module (like intersect1d_nu).

Thank you,
r.


More information about the Numpy-discussion mailing list