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 #2554 is a reply to message #2111] Tue, 26 July 1994 09:39 Go to previous message
andy is currently offline  andy
Messages: 31
Registered: November 1993
Member
In article <CtJyp1.77x@ngdc.noaa.gov>, greg@farpoint.ngdc.noaa.gov (Greg Ushomirskiy) writes:
> A while ago there has been a discussion on what is the fastest way to find common
> elements in a pair of arrays. Of course, then I didn't listen, and now I need to
> do just that -- find common values in two arrays of longs. Since the articles
> describing the solution already expired, can someone post a summary?
>
> Thanks...
>
>
> --
> Greg Ushomirskiy #include <std_disclaimer.h>
> greg@farpoint.ngdc.noaa.gov
> National Geophysical Data Center
> NOAA, US. Department of Commerce


Ahhh! Those were the days. What a good discussion.
I don't know if this is the *BEST* solution, but it
is one that I found useful. I added some comments
but did not change the workings.

; General purpose routine to return the 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

function same, a, b

if (n_params() lt 1) then message, ' Usage... result = same(a, b)'

ab = [a(uniq(a,sort(a))), b(uniq(b,sort(b)))] ;Combine unique elements of a,b
ab = ab(sort(ab)) ;Sort the combined elements
nab = n_elements(ab)

diff = ab(1:nab-1) - ab(0:nab-2)
ind = where(diff eq 0) ;Find repeated elements

if (ind(0) eq -1) then return, -999
if (ind(0) ne -1) then return, ab(ind)

end


--
,__o Andrew F. Loughe (Mail Code 971)
-\_<, NASA Goddard Space Flight Center phone: (301) 286-5899
(*)/'(*) Greenbelt, MD 20771 email: andy.loughe@gsfc.nasa.gov
[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: Sat Oct 11 02:40:36 PDT 2025

Total time taken to generate the page: 0.32044 seconds