Re: The intersection of 2 arrays [message #8354] |
Mon, 03 March 1997 00:00 |
David Foster
Messages: 341 Registered: January 1996
|
Senior Member |
|
|
Phil Williams wrote:
>
> I know that I can find the union of two arrays by doing something like
>
> temp = [array1, array2]
> union = temp(uniq(temp, sort(temp)))
>
> But how would I go about finding the intersection of the two arrays?
> i.e. Is there a nice vector way of doing it rather than brute force?
> (aka: Which IDL function did I miss this time?)
By brute force, do you mean something like:
temp = [ array1(uniq(array1,sort(array1))), $
array2(uniq(array2,sort(array2))) ]
temp = temp( sort(temp) )
inter = temp(where( temp eq shift(temp,1)))
If someone has a better/faster method please share it!
Dave
PS>
I've written a FIND_ELEMENTS.PRO that returns subscripts of
elements in one array that are found in another array, like:
ind = Find_Elements(Array, Tofind [, Adjust_array])
This searches Array for values in Tofind, returning
the subscripts of Array for those values found. The optional
argument Adjust_array contains the same number of elements as
Tofind, and is a way of allowing you to adjust the elements
of Array (the values of Adjust_array and Tofind correspond).
We use this for finding and adjusting Regions-of-Interest in
images which have a limited number of discrete values. It
uses a loop to search Array for each value of Tofind, so
it's not real fast for large search arrays. [If anyone has
an array-oriented method I'd love to see it!!]
If anyone would find this useful let me know (email).
Dave
--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~
David S. Foster Univ. of California, San Diego
Programmer/Analyst Brain Image Analysis Laboratory
foster@bial1.ucsd.edu Department of Psychiatry
(619) 622-5892 8950 Via La Jolla Drive, Suite 2200
La Jolla, CA 92037
[ UCSD Mail Code 0949 ]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~
|
|
|