Velocity Reviews > matching elements of numeric arrays

# matching elements of numeric arrays

daniel.neilson@gmail.com
Guest
Posts: n/a

 10-13-2005
I have two one-dimensional Numeric arrays, and I need to know the
indices in the second array of elements from the first.

a=array([2,4,6])
b=array([2,3,4,5,6])

i want a function match that does this:

>>> match(a,b)

array([0,2,4])

i have something that works, but it involves converting things to lists
and using 'in', and it is rather slow. if someone could point me to a
better solution, i would appreciate it.

Robert Kern
Guest
Posts: n/a

 10-13-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> I have two one-dimensional Numeric arrays, and I need to know the
> indices in the second array of elements from the first.
>
>
> a=array([2,4,6])
> b=array([2,3,4,5,6])
>
> i want a function match that does this:
>
>
>>>>match(a,b)

>
> array([0,2,4])
>
> i have something that works, but it involves converting things to lists
> and using 'in', and it is rather slow. if someone could point me to a
> better solution, i would appreciate it.

If b is sorted, then you can use Numeric.searchsorted(b, a).

Otherwise:

In [28]: import Numeric as N

In [29]: a = N.array([2,4,6])

In [30]: b = N.array([2,3,4,5,6])

In [31]: match = N.equal.outer(a, b)

In [32]: idx = N.compress(N.sum(match), N.arange(len(b)))

In [33]: idx
Out[33]: array([0, 2, 4])

--
Robert Kern
(E-Mail Removed)

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter

Claudio Grondi
Guest
Posts: n/a

 10-14-2005
<(E-Mail Removed)> wrote in
news:(E-Mail Removed) oups.com...
> I have two one-dimensional Numeric arrays, and I need to know the
> indices in the second array of elements from the first.
>
>
> a=array([2,4,6])
> b=array([2,3,4,5,6])
>
> i want a function match that does this:
>
> >>> match(a,b)

> array([0,2,4])
>
> i have something that works, but it involves converting things to lists
> and using 'in', and it is rather slow. if someone could point me to a
> better solution, i would appreciate it.

I have no idea about Numeric array module, so I will use
the built-in Python one for it (how about the speed if
compared to Numeric arrays?):

from array import array
a = array('I', [2,4,6])
b = array('I', [2,3,4,5,6])

def match(a,b):
retVal = array('I')
for item in a:
retVal.append(b.index(item))
return retVal

print a
print b
print str(match(a,b))

Outputs:

array('I', [2L, 4L, 6L])
array('I', [2L, 3L, 4L, 5L, 6L])
array('I', [0L, 2L, 4L])

Claudio

daniel.neilson@gmail.com
Guest
Posts: n/a

 10-14-2005
Thanks a lot. It was this equal.outer idea that I was missing.

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post Philipp Java 21 01-20-2009 08:33 AM jobs ASP .Net 2 07-22-2007 12:32 AM darrel ASP .Net 4 07-19-2007 09:57 PM Nick Black Ruby 3 01-23-2007 03:40 PM ief@specialfruit.be C++ 5 06-30-2005 01:08 PM