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

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

improvements of headers (alignments of IDL prompt in examples)

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