[Numpy-discussion] setmember1d memory leak?

Robert Cimrman cimrman3 at ntc.zcu.cz
Thu Jan 25 05:30:53 CST 2007


Charles R Harris wrote:
> On 1/24/07, Charles R Harris <charlesr.harris at gmail.com> wrote:
>>
>>
>>
>> On 1/24/07, Robert Cimrman <cimrman3 at ntc.zcu.cz> wrote:
>> >
>> > Robert Kern wrote:
>> > > Robert Cimrman wrote:
>> > >> Or you could just call unique1d prior to your call to setmember1d -
>> > it
>> > >> was meant to be used that way... you would not loose much speed that
>> > >> way, IMHO.
>> > >
>> > > But that doesn't do what they want. They want a function that gives
>> > the mask
>> > > against their original array of the elements that are in the other
>> > array. The
>> > > result of
>> > >
>> > >   setmember1d(unique1d(ar1), unique1d(ar2))
>> > >
>>
>>
> <snip>
> 
> For instance
> 
> In [7]: def countmembers(a1, a2) :
>   ...:     a = sort(a2)
>   ...:     il = a.searchsorted(a1, side='l')
>   ...:     ir = a.searchsorted(a1, side='r')
>   ...:     return ir - il
>   ...:
> 
> In [8]: a2 = random.randint(0,10,(100,))
> 
> In [9]: a1 = arange(11)
> 
> In [11]: a2 = random.randint(0,5,(100,))
> 
> In [12]: a1 = arange(10)
> 
> In [13]: countmembers(a1,a2)
> Out[13]: array([16, 28, 16, 25, 15,  0,  0,  0,  0,  0])
> 
> 
> The subtraction can be replaced by != to get a boolean mask.

It looks good! Isn't it faster than setmember1d for unique input arrays?
I do not like setmember1d much (it is long unlike other functions in
arraysetops and looks clumsy to me now and I do not understand it
anymore...), so feel free to replace it.

BTW. setmember1d gives me the same mask as countmembers for several
non-unique inputs I tried...

r.


More information about the Numpy-discussion mailing list