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

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