[Numpy-discussion] setmember1d memory leak?

Charles R Harris charlesr.harris at gmail.com
Wed Jan 24 10:30:33 CST 2007


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))
> >
> > is a mask against
> >
> >   unique1d(ar1)
> >
> > not
> >
> >   ar1
> >
> > as they want.
>
> I see. I was thinking in terms of 'set member' - in set one assumes
> unique elements. A good name for this kind of function would be
> 'arraymember'.
>
> Naive pseudo-implementation:
>
> import numpy as nm
> def arraymember1d( ar1, ar2 ):
>
>     ar = ar2.copy().sort()
>     indx = findsorted( ar2, ar1 )
>     flag = nm.zeros( ar1.shape, dtype = nm.bool )
>     flag[indx] = True
>
>     return flag
>
> Note that nm.searchsorted cannot be used here - a new function
> ('findsorted'?) would be needed.


Maybe something  like

countelements(ar1, ar2) :
    a = sort(ar2)
    il = a.searchsorted(ar1, side='left')
    ir = a.searchsorted(ar1, side='right')
    return ir - il

which returns an array containing the number of times the corresponding
element of ar1 is contained in ar2.


Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20070124/7aeb27db/attachment.html 


More information about the Numpy-discussion mailing list