comparison rDiff/src/octave/intersect.m @ 0:0f80a5141704

version 0.3 uploaded
author vipints
date Thu, 14 Feb 2013 23:38:36 -0500
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:0f80a5141704
1 ## Copyright (C) 2000-2012 Paul Kienzle
2 ## Copyright (C) 2008-2009 Jaroslav Hajek
3 ##
4 ## This file is part of Octave.
5 ##
6 ## Octave is free software; you can redistribute it and/or modify it
7 ## under the terms of the GNU General Public License as published by
8 ## the Free Software Foundation; either version 3 of the License, or (at
9 ## your option) any later version.
10 ##
11 ## Octave is distributed in the hope that it will be useful, but
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ## General Public License for more details.
15 ##
16 ## You should have received a copy of the GNU General Public License
17 ## along with Octave; see the file COPYING. If not, see
18 ## <http://www.gnu.org/licenses/>.
19
20 ## -*- texinfo -*-
21 ## @deftypefn {Function File} {} intersect (@var{a}, @var{b})
22 ## @deftypefnx {Function File} {[@var{c}, @var{ia}, @var{ib}] =} intersect (@var{a}, @var{b})
23 ##
24 ## Return the elements in both @var{a} and @var{b}, sorted in ascending
25 ## order. If @var{a} and @var{b} are both column vectors return a column
26 ## vector, otherwise return a row vector.
27 ## @var{a}, @var{b} may be cell arrays of string(s).
28 ##
29 ## Return index vectors @var{ia} and @var{ib} such that @code{a(ia)==c} and
30 ## @code{b(ib)==c}.
31 ##
32 ## @end deftypefn
33 ## @seealso{unique, union, setxor, setdiff, ismember}
34
35 function [c, ia, ib] = intersect (a, b, varargin)
36
37 if (nargin < 2 || nargin > 3)
38 print_usage ();
39 endif
40
41 ## [a, b] = validargs ("intersect", a, b, varargin{:});
42
43 if (isempty (a) || isempty (b))
44 c = ia = ib = [];
45 else
46 ## form a and b into sets
47 if (nargout > 1)
48 [a, ja] = unique (a, varargin{:});
49 [b, jb] = unique (b, varargin{:});
50 else
51 a = unique (a, varargin{:});
52 b = unique (b, varargin{:});
53 endif
54
55 if (nargin > 2)
56 c = [a; b];
57 [c, ic] = sortrows (c);
58 ii = find (all (c(1:end-1,:) == c(2:end,:), 2));
59 c = c(ii,:);
60 len_a = rows (a);
61 else
62 c = [a(:); b(:)];
63 [c, ic] = sort (c); ## [a(:);b(:)](ic) == c
64 if (iscellstr (c))
65 ii = find (strcmp (c(1:end-1), c(2:end)));
66 else
67 ii = find (c(1:end-1) == c(2:end));
68 endif
69 c = c(ii);
70 len_a = length (a);
71 endif
72
73 if (nargout > 1)
74 ia = ja(ic(ii)); ## a(ia) == c
75 ib = jb(ic(ii+1) - len_a); ## b(ib) == c
76 endif
77
78 if (nargin == 2 && (size (b, 1) == 1 || size (a, 1) == 1))
79 c = c.';
80 endif
81 endif
82
83 endfunction
84
85
86 %!# Test the routine for index vectors ia and ib
87 %!test
88 %! a = [3 2 4 5 7 6 5 1 0 13 13];
89 %! b = [3 5 12 1 1 7];
90 %! [c,ia,ib] = intersect(a,b);
91 %! assert(c,[1 3 5 7]);
92 %! assert(ia,[8 1 7 5]);
93 %! assert(ib,[5 1 2 6]);
94 %! assert(a(ia),c);
95 %! assert(b(ib),c);
96 %!test
97 %! a = [1,1,2;1,4,5;2,1,7];
98 %! b = [1,4,5;2,3,4;1,1,2;9,8,7];
99 %! [c,ia,ib] = intersect(a,b,'rows');
100 %! assert(c,[1,1,2;1,4,5]);
101 %! assert(ia,[1;2]);
102 %! assert(ib,[3;1]);
103 %! assert(a(ia,:),c);
104 %! assert(b(ib,:),c);
105 %!test
106 %! a = [1 1 1 2 2 2];
107 %! b = [1 2 3 4 5 6];
108 %! c = intersect(a,b);
109 %! assert(c, [1,2]);
110 %!test
111 %! a = [1 2 3 4; 5 6 7 8; 9 10 11 12];
112 %! [b, ia, ib] = intersect(a, a, "rows");
113 %! assert(b, a);
114 %! assert(ia, [1:3]');
115 %! assert(ib, [1:3]');