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

Home » Public Forums » archive » 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 #2112 is a reply to message #2111] Wed, 01 June 1994 23:26 Go to previous messageGo to previous message
stl is currently offline  stl
Messages: 70
Registered: February 1994
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?

I just discovered (a few weeks ago) that this is a very usefull
function! I was forced to use one loop (not sure if used one or two
because you mention above 'loops'). If there is an array function I
would LOVE to see it cause I need to do this LOTS of times, with arrays
of up to 10,000 elements!

Here is what I did:
index = 0
for i = 0,n_elements(b)-1 do begin
set = where(a eq b(i))
if set(0) ne -1 then index = [index,set]
endfor
;get ride of the first 0!
if n_elements(index) gt 1 then index = index[1:*] $
else index = -1

for huge array, this is a bummer to have to do over and over! I am
interested in an discussion and better solutions to this seamingly
trivial but painfull little problem.

-stephen
--
Stephen C Strebel / SKI TO DIE
stl@maz.sma.ch / and
Swiss Meteorological Institute, Zuerich / LIVE TO TELL ABOUT IT
01 256 93 85 / (and pray for snow)
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Finding common elements in two arrays
Next Topic: Please help with buggy widget code

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

Current Time: Fri Oct 10 22:32:55 PDT 2025

Total time taken to generate the page: 0.88356 seconds