[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: setintersection

Well, to answer my own question, if anyone's interested...
After thinking about it a bit, I was able to modify the
setintersection function to produce the desired result.

Like the other routines, it "operates on sets represented
by arrays of positive integers."

There's probably a way to make it more efficient, but here
it is.

;; finds the indeces in sets <a> and <b> of elements
;; in the intersection of sets <a> and <b>
pro iSetIntersection, a, b, ia=ia, ib=ib
  ;; use the full range
  minab = min(a, MAX=maxa) < min(b, MAX=maxb)
  maxab = maxa > maxb

  ;; If either set is empty return null sets
  if minab lt 0 then begin
    ia = -1
    ib = -1

  ;; find intersection
  r = histogram(a, MIN=minab, MAX=maxab) < 1 and $
   histogram(b, MIN=minab, MAX=maxab) < 1

  ;; indeces of elements in intersection
  ia = where(r(a-minab) gt 0)
  ib = where(r(b-minab) gt 0)

And here's the original setintersection

FUNCTION SetIntersection, a, b
  minab = min(a, MAX=maxa) > min(b, MAX=maxb) ;Only need intersection of
  maxab = maxa < maxb

  ;; If either set is empty, or their ranges don't intersect: result =
  if maxab lt minab or maxab lt 0 then return, -1
  r = where((histogram(a, MIN=minab, MAX=maxab) ne 0) and  $
            (histogram(b, MIN=minab, MAX=maxab) ne 0), count)
  if count eq 0 then return, -1 else return, r + minab

Jonathan Joseph wrote:
> Hi, I've grabbed the useful functions
> SetIntersection(a,b)      ; Common elements
> SetUnion(a,b)             ; Elements in either set
> SetDifference(a,b)        ; Elements in A but not in B
> from David Fanning's web page
> http://www.dfanning.com/tips/set_operations.html
> But, what I really would like is a function
> that would return the indeces in set A (or B) of
> the intersection of A and B.
> Does anyone know an efficient way to get this, or
> will I have to resort to a loop?
> -Jonathan