473,653 Members | 3,000 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

matching elements of numeric arrays

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

so if i had:

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.

Oct 13 '05 #1
3 2370
<da************ @gmail.com> wrote in
news:11******** **************@ g49g2000cwa.goo glegroups.com.. .
I have two one-dimensional Numeric arrays, and I need to know the
indices in the second array of elements from the first.

so if i had:

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
Oct 13 '05 #2
da************@ gmail.com wrote:
I have two one-dimensional Numeric arrays, and I need to know the
indices in the second array of elements from the first.

so if i had:

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.searchs orted(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.su m(match), N.arange(len(b) ))

In [33]: idx
Out[33]: array([0, 2, 4])
--
Robert Kern
rk***@ucsd.edu

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

Oct 14 '05 #3
Thanks a lot. It was this equal.outer idea that I was missing.

Oct 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2729
by: Duncan Smith | last post by:
I am having problems saving arrays to file. e.g. >>> t1 array() >>> t2 array() >>> t3 array() >>> t4 array(,
1
1757
by: Jacek Generowicz | last post by:
The Numeric docs state that "Subclassing Numeric arrays is not possible due to a limitation of Python." What is this limitation? My first guess is that it is the unsbclassability of built-in types, which was addressed by the introduction of new-style classes ... and, indeed, Numeric.ArrayType.__bases__ lists object, so it looks like a new style class and therefore it seems to me that it should be subclassable. Yet, if I try to subclass...
0
1403
by: Travis Oliphant | last post by:
Numarray is making great progress and is quite usable for many purposes. An idea that was championed by some is that the Numeric code base would stay static and be replaced entirely by Numarray. However, Numeric is currently used in a large installed base. In particular SciPy uses Numeric as its core array. While no doubt numarray arrays will be supported in the future, the speed of the less bulky Numeric arrays and the typical case...
5
1492
by: George Sakkis | last post by:
What's the fastest and most elegant equivalent of zip() in Numeric/Numarray between two equally sized 1D arrays ? That is, how to combine two (N,)-shaped arrays to one (N,2) (or (2,N)) shaped ? I expect the fastest and the most elegant idiom to be identical, as it is usually the case in this excellent library, but if not, both would be useful to know. Thanks, George
8
1976
by: JS | last post by:
I am trying to add an element to an array like this: var ty = ; zz = "ty"; var ab = null; zz+=""; ab = eval(zz); document.write(ab.length);
11
2966
by: solandre | last post by:
hi there, i have to find matches in two sorted arrays. there are two good possibilities as a temporary solution, performing different, depending on the inner structure of the data. but i try to find a better one. ..possibility one ... ...is running through the arrays in a zig-zag, always taking the an element from array A and searching the remaining elements of array B sequentially for an equal or a higher match.
7
1651
by: Sheldon | last post by:
Hi, I have the following loop that I think can be written to run faster in Numeric. I am currently using Numeric. range_va = main.xsize= 600 main.ysize= 600 #msgva is an (600x600) Numeric array with mutiple occurrences of the values in range_va #sat_id is an (600x600) Numeric array with values ranging from -2 to 2
2
2273
by: goetzie | last post by:
I am using Python 2.4.1 and Numeric 23.8 and running on Windows XP. I am passing a Numeric array of strings (objects) to a C Extension module using the following python code: import Numeric import TestDLL # my C Extension Module a = Numeric.array(, 'O' ) print 'a =', a print 'type a =', type(a)
4
3050
by: Thomas Mlynarczyk | last post by:
Hello, I have two arrays like this: $aSearch = array( 'A', 'B', 'C.D', 'E', 'F' ); $aSubject = array( 'A' =0, 'A.B' =1, 'X' =2, 'C.D.E' =3, 'A.B.C' => 4 ); Now I want to search $aSubject for the longest key that consists of the first x elements of $aSearch concatenated by "." (where x = 1...count(
0
8283
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8811
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8704
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8470
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6160
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5620
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4147
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4291
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1914
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.