source: trunk/SRC/Matrix/inter.pro @ 325

Last change on this file since 325 was 325, checked in by pinsard, 16 years ago

modification of some headers (+some corrections) to prepare usage of the new idldoc

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