[Numpy-discussion] help vectorizing something
Charles R Harris
charlesr.harris@gmail....
Fri Oct 24 17:12:46 CDT 2008
On Fri, Oct 24, 2008 at 3:48 PM, Mathew Yeates <mat.yeates@gmail.com> 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20081024/0569ad64/attachment.html
More information about the Numpy-discussion
mailing list