[2] | 1 | ;+ |
---|
| 2 | ; |
---|
[232] | 3 | ; @file_comments |
---|
[373] | 4 | ; find the intersection between 2 matrices of whole numbers |
---|
[2] | 5 | ; |
---|
[378] | 6 | ; see also <pro>different</pro> and <pro>union</pro> |
---|
[373] | 7 | ; |
---|
[232] | 8 | ; @categories |
---|
[157] | 9 | ; Calculation |
---|
[2] | 10 | ; |
---|
[237] | 11 | ; @param a {in}{required} |
---|
| 12 | ; arrays of positive integers, which need not to be |
---|
[163] | 13 | ; sorted. Duplicate elements are ignored, as they have no effect on the |
---|
[31] | 14 | ; result |
---|
[2] | 15 | ; |
---|
[237] | 16 | ; @param b {in}{required} |
---|
| 17 | ; see a |
---|
[2] | 18 | ; |
---|
[237] | 19 | ; @returns |
---|
[373] | 20 | ; an array containing the set of values in both a and b. |
---|
[2] | 21 | ; |
---|
[237] | 22 | ; The empty set is denoted by an array with the first element equal to |
---|
[2] | 23 | ; -1. |
---|
| 24 | ; |
---|
[237] | 25 | ; @restrictions |
---|
| 26 | ; These functions will not be efficient on sparse sets with wide |
---|
| 27 | ; ranges, as they trade memory for efficiency. |
---|
| 28 | ; The <proidl>HISTOGRAM</proidl> function |
---|
[2] | 29 | ; is used, which creates arrays of size equal to the range of the |
---|
| 30 | ; resulting set. |
---|
| 31 | ; |
---|
[232] | 32 | ; @examples |
---|
[2] | 33 | ; |
---|
[371] | 34 | ; IDL> a = [2,4,6,8] |
---|
| 35 | ; IDL> b = [6,1,3,2] |
---|
[373] | 36 | ; IDL> res=inter(a,b) |
---|
| 37 | ; 2 6 |
---|
| 38 | ; Right because 2 and 6 are in a and B. |
---|
[371] | 39 | ; |
---|
[232] | 40 | ; @history |
---|
| 41 | ; <a href="http://www.dfanning.com/tips/set_operations.html"/> |
---|
[2] | 42 | ; |
---|
[232] | 43 | ; @version |
---|
| 44 | ; $Id$ |
---|
[133] | 45 | ; |
---|
[2] | 46 | ;- |
---|
| 47 | FUNCTION inter, a, b |
---|
| 48 | ; |
---|
[114] | 49 | compile_opt idl2, strictarrsubs |
---|
| 50 | ; |
---|
[2] | 51 | case 1 of |
---|
| 52 | n_elements(a) EQ 0:return, -1 |
---|
| 53 | n_elements(b) EQ 0:return, -1 |
---|
| 54 | n_elements(a) EQ 1 AND n_elements(b) NE 1: $ |
---|
| 55 | if (where(b EQ a[0]))[0] EQ -1 then return, -1 ELSE return, a[0] |
---|
| 56 | n_elements(b) EQ 1 AND n_elements(a) NE 1: $ |
---|
| 57 | if (where(a EQ b[0]))[0] EQ -1 then return, -1 ELSE return, b[0] |
---|
| 58 | n_elements(a) EQ 1 AND n_elements(b) EQ 1: $ |
---|
| 59 | if (where(a[0] EQ b[0]))[0] EQ -1 then return, -1 ELSE return, a[0] |
---|
| 60 | ELSE: |
---|
| 61 | ENDCASE |
---|
| 62 | ; |
---|
| 63 | minab = Min(a, Max=maxa) > Min(b, Max=maxb) ;Only need intersection of ranges |
---|
| 64 | maxab = maxa < maxb |
---|
| 65 | |
---|
| 66 | ; If either set is empty, or their ranges don't intersect: result = NULL. |
---|
| 67 | |
---|
| 68 | IF maxab LT minab OR maxab LT 0 THEN RETURN, -1 |
---|
| 69 | r = Where(Histogram(a, Min=minab, Max=maxab) $ |
---|
| 70 | *Histogram(b, Min=minab, Max=maxab), count) |
---|
| 71 | |
---|
| 72 | IF count EQ 0 THEN RETURN, -1 ELSE RETURN, r + minab |
---|
| 73 | END |
---|