[Numpy-discussion] help vectorizing something
Charles R Harris
charlesr.harris at gmail.com
Fri Oct 24 19:03:33 EDT 2008
On Fri, Oct 24, 2008 at 4:23 PM, Mathew Yeates <mat.yeates at gmail.com> wrote:
> hmmmm. I don't understand the result.
>
> If
> a=array([ 1, 2, 3, 7, 10]) and b=array([ 1, 2, 3, 8, 10])
>
> I want to get the result [0,1,2,4] but[searchsorted(a,b) produces
> [0,1,2,4,4] ?? and searchsorted(b,a) produces [0,1,2,3,4]
>
Because b isn't a subset of a. You can get around this by counting the
number, i.e.,
cnt = searchsorted(a,b, side='right') - seachsorted(a, b, side='left')
so that
In [1]: a = array([ 1, 2, 3, 7, 10])
In [2]: b = array([ 1, 2, 3, 8, 10])
In [3]: il = searchsorted(a, b, side='left')
In [4]: ir = searchsorted(a, b, side='right')
In [5]: compress(ir - il, il)
Out[5]: array([0, 1, 2, 4])
Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20081024/7adbc540/attachment.html>
More information about the NumPy-Discussion
mailing list