comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Re: Finding Common Elements in Two Arrays
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Return to the default flat view Create a new topic Submit Reply
Re: Finding Common Elements in Two Arrays [message #2109 is a reply to message #2106] Thu, 02 June 1994 05:04 Go to previous message
dutch is currently offline  dutch
Messages: 8
Registered: June 1992
Junior Member
In article <1JUN199416454238@stars.gsfc.nasa.gov>, kucera@stars.gsfc.nasa.gov (Terry Kucera) writes:
>> I'm looking for a quick way to compare two arrays in IDL, A and B,
>> and determine which elements of B are also in A,
>> so if:
>> A=[2,1,3,5,3,8,2,5]
>> B=[3,4,2,8,7,8]
>> I would get [0,2,3,5], because 3, 2, and 8 are in A as well as B.
>>
>> I can do this with loops, but that takes too long for big arrays. Does anyone
>> have a way to do this using array functions or perhaps an external routine?
>> Terry Kucera
>> kucera@stars.gsfc.nasa.gov
>>

Perhaps the following function is what you are looking for. As it is currently
written, the function returns the VALUES of repeated elements, rather than
the INDICES into the first vector (as in your example), but this is a trivial
modification, if necessary!.
The only hiccup, is what to return if there are no repeated elements,
in my case I have chosen to return the value -999. This function is a rewrite
of a similar function in MATLAB. (in MATLAB, the function can return the empty
set as a null vector (with zero elements). In IDL, the function could be modified to return a second argument, which indicates the number of elements in the intersection.

Rough benchmarks on our Vax 4090 are as follows:
2 random vectors each of length 10,000 elements - about 1 second
2 random vectors each of length 100,000 elements - about 12 seconds

I expect this is considerably better than alternative solutions using one or
more FOR loops!.



---------- 8< start: intersection.pro >8 -------------

FUNCTION intersection,a,b
;+
; general purpose routine to return the intersection
; (i.e. common elements) of two vectors. Uses USERLIB
; routine UNIQ to obtain unique elements of a,b
; Written by M.J.Dutch June-1994.
; Centre de Recherches en Physique des Plasmas, EPFL, Switzerland
;-

ab=[a(uniq(a,sort(a))),b(uniq(b,sort(b)))] ; combine unique elements of a,b
ab=ab(sort(ab)) ; sort the combined vector
nab=n_elements(ab)
diff=ab(1:nab-1)-ab(0:nab-2)
ind=where(diff eq 0) ; find repeated elements
if (ind(0) ne -1) then return,ab(ind) else return,-999

end

---------- 8< end: intersection.pro >8 -------------



Enjoy!

-- Michael
###########################################################
# Dr. Michael Dutch email: dutch@eltcv1.epfl.ch #
# Centre de Recherches en Physique des Plasmas #
# Ecole Polytechnique Federale de Lausanne #
# 21 Ave des Bains Aussie.Abroad #
# CH-1007 Lausanne, SWITZERLAND ,--_|\ #
#---------------------------------------- / \ #
# I'd rather have a full bottle in front \_.-*._/ #
# of me than a full frontal lobotomy. v #
###########################################################
[Message index]
 
Read Message
Read Message
Read Message
Previous Topic: IDL hangs with Bessel Function
Next Topic: Modular programming in IDL....trouble

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Sun Oct 12 00:46:45 PDT 2025

Total time taken to generate the page: 1.20848 seconds