[Numpy-discussion] help vectorizing something

2008-10-24 Thread Mathew Yeates
Hi
I  have 2 vectors A and B. For each value in A I want to find the location
in B of the same value. Both A and B have unique elements.

Of course I could something like
For each index of A:
   v =A[index]
   location = numpy.where(B == v)

But I have very large lists and it will take too long.

Thanks to any one of you  vectorization gurus that has any ideas.

Mathew
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] help vectorizing something

2008-10-24 Thread Charles R Harris
On Fri, Oct 24, 2008 at 3:48 PM, Mathew Yeates [EMAIL PROTECTED] wrote:

 Hi
 I  have 2 vectors A and B. For each value in A I want to find the location
 in B of the same value. Both A and B have unique elements.

 Of course I could something like
 For each index of A:
v =A[index]
location = numpy.where(B == v)

 But I have very large lists and it will take too long.


In [1]: A = array([1,2,3])

In [2]: B = array([5,1,3,0,2,4])

In [3]: i = B.argsort()

In [4]: Bsorted = B[i]

In [5]: indices = i[searchsorted(Bsorted,A)]

In [6]: indices
Out[6]: array([1, 4, 2])

Chuck
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] help vectorizing something

2008-10-24 Thread Mathew Yeates
h. 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]

??
Mathew


On Fri, Oct 24, 2008 at 3:12 PM, Charles R Harris [EMAIL PROTECTED]
 wrote:



 On Fri, Oct 24, 2008 at 3:48 PM, Mathew Yeates [EMAIL PROTECTED]wrote:

 Hi
 I  have 2 vectors A and B. For each value in A I want to find the location
 in B of the same value. Both A and B have unique elements.

 Of course I could something like
 For each index of A:
v =A[index]
location = numpy.where(B == v)

 But I have very large lists and it will take too long.


 In [1]: A = array([1,2,3])

 In [2]: B = array([5,1,3,0,2,4])

 In [3]: i = B.argsort()

 In [4]: Bsorted = B[i]

 In [5]: indices = i[searchsorted(Bsorted,A)]

 In [6]: indices
 Out[6]: array([1, 4, 2])

 Chuck



 ___
 Numpy-discussion mailing list
 Numpy-discussion@scipy.org
 http://projects.scipy.org/mailman/listinfo/numpy-discussion


___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] help vectorizing something

2008-10-24 Thread Charles R Harris
On Fri, Oct 24, 2008 at 4:23 PM, Mathew Yeates [EMAIL PROTECTED] wrote:

 h. 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
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion