[2] | 1 | ;------------------------------------------------------------ |
---|
| 2 | ;------------------------------------------------------------ |
---|
| 3 | ;------------------------------------------------------------ |
---|
| 4 | ;+ |
---|
| 5 | ; |
---|
[133] | 6 | ; @file_comments |
---|
[163] | 7 | ; calculate the different elements of 2 matrix of positive whole numbers. |
---|
[2] | 8 | ; |
---|
[157] | 9 | ; @categories |
---|
| 10 | ; Calculation |
---|
[2] | 11 | ; |
---|
[133] | 12 | ; @param a {in}{required} arrays of positive integers, which need |
---|
[2] | 13 | ; not be sorted. Duplicate elements are ignored, as they have no |
---|
| 14 | ; effect on the result |
---|
| 15 | ; |
---|
[133] | 16 | ; @param b {in}{required} see a |
---|
[2] | 17 | ; |
---|
[133] | 18 | ; @returns tableau |
---|
[2] | 19 | ; |
---|
[133] | 20 | ; @restrictions The empty set is denoted by an array with the first element equal to |
---|
[2] | 21 | ; -1. |
---|
| 22 | ; |
---|
[133] | 23 | ; @restrictions These functions will not be efficient on sparse sets with wide |
---|
[2] | 24 | ; ranges, as they trade memory for efficiency. The HISTOGRAM function |
---|
| 25 | ; is used, which creates arrays of size equal to the range of the |
---|
| 26 | ; resulting set. |
---|
| 27 | ; |
---|
[133] | 28 | ; @examples |
---|
[2] | 29 | ; |
---|
| 30 | ; a = [2,4,6,8] |
---|
| 31 | ; b = [6,1,3,2] |
---|
| 32 | ; different(a,b) = [ 4, 8] ; Elements in A but not in B |
---|
| 33 | ; |
---|
[133] | 34 | ; @history http://www.dfanning.com/tips/set_operations.html |
---|
[2] | 35 | ; |
---|
[133] | 36 | ; @version $Id$ |
---|
| 37 | ; |
---|
[2] | 38 | ;- |
---|
| 39 | ;------------------------------------------------------------ |
---|
| 40 | ;------------------------------------------------------------ |
---|
| 41 | ;------------------------------------------------------------ |
---|
| 42 | FUNCTION different, a, b |
---|
[114] | 43 | ; |
---|
| 44 | compile_opt idl2, strictarrsubs |
---|
| 45 | ; |
---|
[2] | 46 | |
---|
| 47 | ; = a and (not b) = elements in A but not in B |
---|
| 48 | |
---|
| 49 | mina = Min(a, Max=maxa) |
---|
| 50 | minb = Min(b, Max=maxb) |
---|
| 51 | IF (minb GT maxa) OR (maxb LT mina) THEN RETURN, a ;No intersection... |
---|
| 52 | r = Where(Histogram(a, Min=mina, Max=maxa) $ |
---|
| 53 | *(1-Histogram(b, Min=mina, Max=maxa)), count) |
---|
| 54 | IF count eq 0 THEN RETURN, -1 ELSE RETURN, r + mina |
---|
| 55 | END |
---|