annotate scripts/set/intersect.m @ 12061:9b9aaacf194d release-3-2-x

dlmread: perform tilde expansion to filename argument
author John W. Eaton <jwe@octave.org>
date Sun, 23 Aug 2009 11:09:17 +0200
parents ef95b930f1cf
children 5edee330d4cb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8920
eb63fbe60fab update copyright notices
John W. Eaton <jwe@octave.org>
parents: 8507
diff changeset
1 ## Copyright (C) 2008, 2009 Jaroslav Hajek
7017
a1dbe9d80eee [project @ 2007-10-12 21:27:11 by jwe]
jwe
parents: 7016
diff changeset
2 ## Copyright (C) 2000, 2006, 2007 Paul Kienzle
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
3 ##
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
4 ## This file is part of Octave.
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
5 ##
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
6 ## Octave is free software; you can redistribute it and/or modify it
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
7 ## under the terms of the GNU General Public License as published by
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6825
diff changeset
8 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6825
diff changeset
9 ## your option) any later version.
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
10 ##
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
11 ## Octave is distributed in the hope that it will be useful, but
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
14 ## General Public License for more details.
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
15 ##
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
16 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6825
diff changeset
17 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6825
diff changeset
18 ## <http://www.gnu.org/licenses/>.
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
19
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
20 ## -*- texinfo -*-
6248
7fad1fad19e1 [project @ 2007-01-22 17:28:17 by jwe]
jwe
parents: 6046
diff changeset
21 ## @deftypefn {Function File} {} intersect (@var{a}, @var{b})
7fad1fad19e1 [project @ 2007-01-22 17:28:17 by jwe]
jwe
parents: 6046
diff changeset
22 ## @deftypefnx {Function File} {[@var{c}, @var{ia}, @var{ib}] =} intersect (@var{a}, @var{b})
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
23 ##
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
24 ## Return the elements in both @var{a} and @var{b}, sorted in ascending
9051
1bf0ce0930be Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
25 ## order. If @var{a} and @var{b} are both column vectors return a column
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
26 ## vector, otherwise return a row vector.
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
27 ##
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
28 ## Return index vectors @var{ia} and @var{ib} such that @code{a(ia)==c} and
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
29 ## @code{b(ib)==c}.
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
30 ##
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
31 ## @end deftypefn
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
32 ## @seealso{unique, union, setxor, setdiff, ismember}
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
33
7920
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
34 function [c, ia, ib] = intersect (a, b, varargin)
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
35
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
36 if (nargin < 2 || nargin > 3)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5821
diff changeset
37 print_usage ();
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
38 endif
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
39
7920
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
40 if (nargin == 3 && ! strcmpi (varargin{1}, "rows"))
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
41 error ("intersect: if a third input argument is present, it must be the string 'rows'");
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
42 endif
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
43
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
44
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
45 if (isempty (a) || isempty (b))
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
46 c = ia = ib = [];
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
47 else
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
48 ## form a and b into sets
7920
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
49 if (nargout > 1)
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
50 [a, ja] = unique (a, varargin{:});
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
51 [b, jb] = unique (b, varargin{:});
9108
b2459d21a207 fix intersect bug
Jaroslav Hajek <highegg@gmail.com>
parents: 9051
diff changeset
52 else
b2459d21a207 fix intersect bug
Jaroslav Hajek <highegg@gmail.com>
parents: 9051
diff changeset
53 a = unique (a, varargin{:});
b2459d21a207 fix intersect bug
Jaroslav Hajek <highegg@gmail.com>
parents: 9051
diff changeset
54 b = unique (b, varargin{:});
6825
59e22e30aff8 [project @ 2007-08-24 15:36:49 by jwe]
jwe
parents: 6248
diff changeset
55 endif
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
56
7920
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
57 if (nargin > 2)
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
58 c = [a; b];
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
59 [c, ic] = sortrows (c);
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
60 ii = find (all (c(1:end-1,:) == c(2:end,:), 2));
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
61 c = c(ii,:);
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
62 else
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
63 c = [a(:); b(:)];
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
64 [c, ic] = sort (c); ## [a(:);b(:)](ic) == c
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
65 if (iscellstr (c))
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
66 ii = find (strcmp (c(1:end-1), c(2:end)));
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
67 else
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
68 ii = find (c(1:end-1) == c(2:end));
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
69 endif
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
70 c = c(ii);
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
71 endif
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
72
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
73
7920
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
74 if (nargout > 1)
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
75 ia = ja(ic(ii)); ## a(ia) == c
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
76 ib = jb(ic(ii+1) - length (a)); ## b(ib) == c
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
77 endif
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
78
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
79
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
80 if (nargin == 2 && (size (b, 1) == 1 || size (a, 1) == 1))
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
81 c = c.';
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
82 endif
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
83 endif
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
84
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
85 endfunction
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
86
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
87
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
88 %!# Test the routine for index vectors ia and ib
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
89 %!test
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
90 %! a = [3 2 4 5 7 6 5 1 0 13 13];
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
91 %! b = [3 5 12 1 1 7];
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
92 %! [c,ia,ib] = intersect(a,b);
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 7920
diff changeset
93 %! assert(c,[1 3 5 7]);
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
94 %! assert(ia,[8 1 7 5]);
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
95 %! assert(ib,[5 1 2 6]);
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
96 %! assert(a(ia),c);
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
97 %! assert(b(ib),c);
7920
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
98 %!test
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
99 %! a = [1,1,2;1,4,5;2,1,7];
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
100 %! b = [1,4,5;2,3,4;1,1,2;9,8,7];
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
101 %! [c,ia,ib] = intersect(a,b,'rows');
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
102 %! assert(c,[1,1,2;1,4,5]);
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
103 %! assert(ia,[1;2]);
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
104 %! assert(ib,[3;1]);
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
105 %! assert(a(ia,:),c);
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
106 %! assert(b(ib,:),c);
9115
ef95b930f1cf Add test for bugfix b2459d21a207
Thomas Weber <thomas.weber.mail@gmail.com>
parents: 9108
diff changeset
107 %!test
ef95b930f1cf Add test for bugfix b2459d21a207
Thomas Weber <thomas.weber.mail@gmail.com>
parents: 9108
diff changeset
108 %! a = [1 1 1 2 2 2];
ef95b930f1cf Add test for bugfix b2459d21a207
Thomas Weber <thomas.weber.mail@gmail.com>
parents: 9108
diff changeset
109 %! b = [1 2 3 4 5 6];
ef95b930f1cf Add test for bugfix b2459d21a207
Thomas Weber <thomas.weber.mail@gmail.com>
parents: 9108
diff changeset
110 %! c = intersect(a,b);
ef95b930f1cf Add test for bugfix b2459d21a207
Thomas Weber <thomas.weber.mail@gmail.com>
parents: 9108
diff changeset
111 %! assert(c, [1,2]);