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

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

improvements of headers (examples and results)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 1.9 KB
Line 
1;+
2;
3; @file_comments
4; find the intersection between 2 matrices of whole numbers
5;
6; see also <pro>difference</pro> and <pro>union</pro>
7;
8; @categories
9; Calculation
10;
11; @param a {in}{required}
12; arrays of positive integers, which need not to be
13; sorted. Duplicate elements are ignored, as they have no effect on the
14; result
15;
16; @param b {in}{required}
17; see a
18;
19; @returns
20; an array containing the set of values in both a and b.
21;
22; The empty set is denoted by an array with the first element equal to
23; -1.
24;
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
29; is used, which creates arrays of size equal to the range of the
30; resulting set.
31;
32; @examples
33;
34;   IDL> a = [2,4,6,8]
35;   IDL> b = [6,1,3,2]
36;   IDL> res=inter(a,b)
37;              2           6
38; Right because 2 and 6 are in a and B.
39;
40; @history
41; <a href="http://www.dfanning.com/tips/set_operations.html"/>
42;
43; @version
44; $Id$
45;
46;-
47FUNCTION inter, a, b
48;
49  compile_opt idl2, strictarrsubs
50;
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;
63minab = Min(a, Max=maxa) > Min(b, Max=maxb) ;Only need intersection of ranges
64maxab = maxa < maxb
65
66   ; If either set is empty, or their ranges don't intersect: result = NULL.
67
68IF maxab LT minab OR maxab LT 0 THEN RETURN, -1
69r = Where(Histogram(a, Min=minab, Max=maxab) $
70          *Histogram(b, Min=minab, Max=maxab), count)
71
72IF count EQ 0 THEN RETURN, -1 ELSE RETURN, r + minab
73END
Note: See TracBrowser for help on using the repository browser.