Re: Finding Common Elements in Two Arrays [message #2104] |
Thu, 02 June 1994 07:23  |
zawodny
Messages: 121 Registered: August 1992
|
Senior Member |
|
|
In article <2skpl4$qr5@reznor.larc.nasa.gov> zawodny@arbd0.larc.nasa.gov (Joseph M Zawodny) writes:
> 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
>
> If you are dealing strictly with integers, I'd use histogram on both
> vectors, multiply them together and then use where to find non-zero values.
>
> Hist_a = histogram(a,max=maximum_expected_value)
> Hist_b = histogram(b,max=maximum_expected_value)
> Inter = where(Hist_a*Hist_b)
>
> and Inter should then contain the array you seek.
No, Sorry, I mis read your post. Inter tells you the value of the elemnts that
are both arrays, not which elements are the same. Hist_b does, however,
contain this information. You will also have to use the REVERSE_INDICES keyword
in the second histogram call. Basically you'd have to use Inter and Hist_b
jointly to construct an array of subscripts.
--
Joseph M. Zawodny (KO4LW) NASA Langley Research Center
Internet: zawodny@arbd0.larc.nasa.gov MS-475, Hampton VA, 23681-0001
Packet: ko4lw@n4hog.va.usa
|
|
|