X
PrevPrev Go to previous topic
NextNext Go to next topic
Last Post 27 Mar 2008 03:39 PM by  anon
Intersection of twoTwo Arrays
 1 Replies
Sort:
You are not authorized to post a reply.
Author Messages

anon



New Member


Posts:
New Member


--
27 Mar 2008 03:39 PM
    Hello All, I am trying to find the interesection between two arrays (not of the same size)? Does anyone know a function that can perform it. For Example: A =[1,2,8,9,10,3,4,5] B=[5,3,4,11,12] Function should return [3,4,5] or the location of common elements in A and B. Thank you for your help. Regards, Vijay

    Deleted User



    New Member


    Posts:
    New Member


    --
    27 Mar 2008 03:39 PM
    Below are some elegant set-processing functions that were created many years ago by IDL's founder, David Stern, but were never permanently built into our language. The one relevant to your question is clearly 'SetIntersection'. ;A somewhat belated reply to the numerous postings on finding the ;common elements of vectors: ; ;> Given vectors of the type... ;> ;> a = [1,2,3,4,5] ;> b = [3,4,5,6,7] ;> ;> What is the most efficient way to determine which values that occur in ;> a also occur in b (i.e., the values [3,4,5] occur in both a and b). ;> ; ;Below appear three IDL functions that operate on sets represented by ;arrays of positive integers. The SetIntersection(a,b) function ;returns the common elements, SetUnion(a,b) returns all unique elements ;in both arguments, and SetDifference(a,b) returns the elements ;(members) in a but not in b. ; ;It is faster than previously published functions, e.g. contain() and ;find_elements(). ; ;Hope this helps, ; ;Research Systems, Inc. ;_________________________________________________________________ ; Set operators. Union, Intersection, and Difference (i.e. return ; members of A that are not in B.) ; ; These functions operate on arrays of positive integers, which need ; not be sorted. Duplicate elements are ignored, as they have no ; effect on the result. ; ; The empty set is denoted by an array with the first element equal to ; -1. ; ; These functions will not be efficient on sparse sets with wide ; ranges, as they trade memory for efficiency. The HISTOGRAM function ; is used, which creates arrays of size equal to the range of the ; resulting set. ; For example: ; a = [2,4,6,8] ; b = [6,1,3,2] ; SetIntersection(a,b) = [ 2, 6] ; Common elements ; SetUnion(a,b) = [ 1, 2, 3, 4, 6, 8] ; Elements in either set ; SetDifference(a,b) = [ 4, 8] ; Elements in A but not in B ; SetIntersection(a,[3,5,7]) = -1 = Null Set FUNCTION SetUnion, a, b if a[0] lt 0 then return, b ;A union NULL = a if b[0] lt 0 then return, a ;B union NULL = b return, where(histogram([a,b], OMIN = omin)) + omin ;Return combined set end FUNCTION SetIntersection, a, b minab = min(a, MAX=maxa) > min(b, MAX=maxb) ;Only need intersection of ranges maxab = maxa James Jones
    You are not authorized to post a reply.