Mercurial > hg > octave-lyh
annotate scripts/set/intersect.m @ 17491:6e21e858d677
gcbf.m: Ignore unused returned argument from gcbo with '~'.
* scripts/plot/gcbf.m: Ignore unused returned argument from gcbo with '~'.
author | Rik <rik@octave.org> |
---|---|
date | Wed, 25 Sep 2013 08:52:30 -0700 |
parents | b81b9d079515 |
children |
rev | line source |
---|---|
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
1 ## Copyright (C) 2000-2012 Paul Kienzle |
11523 | 2 ## Copyright (C) 2008-2009 Jaroslav Hajek |
5820 | 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 | |
7016 | 8 ## the Free Software Foundation; either version 3 of the License, or (at |
9 ## your option) any later version. | |
5820 | 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 | |
7016 | 17 ## along with Octave; see the file COPYING. If not, see |
18 ## <http://www.gnu.org/licenses/>. | |
5820 | 19 |
20 ## -*- texinfo -*- | |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
21 ## @deftypefn {Function File} {} intersect (@var{a}, @var{b}) |
6248 | 22 ## @deftypefnx {Function File} {[@var{c}, @var{ia}, @var{ib}] =} intersect (@var{a}, @var{b}) |
5820 | 23 ## |
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 | 26 ## vector, otherwise return a row vector. |
10088
5edee330d4cb
better argument checking and handling in set functions
Jaroslav Hajek <highegg@gmail.com>
parents:
9115
diff
changeset
|
27 ## @var{a}, @var{b} may be cell arrays of string(s). |
5820 | 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 | |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
35 function [c, ia, ib] = intersect (a, b, varargin) |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
36 |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
37 if (nargin < 2 || nargin > 3) |
6046 | 38 print_usage (); |
5820 | 39 endif |
40 | |
10088
5edee330d4cb
better argument checking and handling in set functions
Jaroslav Hajek <highegg@gmail.com>
parents:
9115
diff
changeset
|
41 [a, b] = validargs ("intersect", a, b, varargin{:}); |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
42 |
5820 | 43 if (isempty (a) || isempty (b)) |
44 c = ia = ib = []; | |
45 else | |
46 ## form a and b into sets | |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
47 if (nargout > 1) |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
48 [a, ja] = unique (a, varargin{:}); |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
49 [b, jb] = unique (b, varargin{:}); |
9108 | 50 else |
51 a = unique (a, varargin{:}); | |
52 b = unique (b, varargin{:}); | |
6825 | 53 endif |
5820 | 54 |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
55 if (nargin > 2) |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
56 c = [a; b]; |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
57 [c, ic] = sortrows (c); |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
58 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
|
59 c = c(ii,:); |
14148
402acc45350e
intersect.m: Properly handle the "rows" case with more than 1 output arg (bug #35247)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
60 len_a = rows (a); |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
61 else |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
62 c = [a(:); b(:)]; |
17344
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
14872
diff
changeset
|
63 [c, ic] = sort (c); # [a(:);b(:)](ic) == c |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
64 if (iscellstr (c)) |
10549 | 65 ii = find (strcmp (c(1:end-1), c(2:end))); |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
66 else |
10549 | 67 ii = find (c(1:end-1) == c(2:end)); |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
68 endif |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
69 c = c(ii); |
14148
402acc45350e
intersect.m: Properly handle the "rows" case with more than 1 output arg (bug #35247)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
70 len_a = length (a); |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
71 endif |
5820 | 72 |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
73 if (nargout > 1) |
17344
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
14872
diff
changeset
|
74 ia = ja(ic(ii)); # a(ia) == c |
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
14872
diff
changeset
|
75 ib = jb(ic(ii+1) - len_a); # b(ib) == c |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
76 endif |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
77 |
14872
c2dbdeaa25df
maint: use rows() and columns() to clarify m-files.
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
78 if (nargin == 2 && (rows (b) == 1 || rows (a) == 1)) |
5820 | 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]; | |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14148
diff
changeset
|
90 %! [c,ia,ib] = intersect (a, b); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14148
diff
changeset
|
91 %! assert (c, [1 3 5 7]); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14148
diff
changeset
|
92 %! assert (ia, [8 1 7 5]); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14148
diff
changeset
|
93 %! assert (ib, [5 1 2 6]); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14148
diff
changeset
|
94 %! assert (a(ia), c); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14148
diff
changeset
|
95 %! assert (b(ib), c); |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
96 %!test |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
97 %! 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
|
98 %! b = [1,4,5;2,3,4;1,1,2;9,8,7]; |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14148
diff
changeset
|
99 %! [c,ia,ib] = intersect (a, b, "rows"); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14148
diff
changeset
|
100 %! assert (c, [1,1,2;1,4,5]); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14148
diff
changeset
|
101 %! assert (ia, [1;2]); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14148
diff
changeset
|
102 %! assert (ib, [3;1]); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14148
diff
changeset
|
103 %! assert (a(ia,:), c); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14148
diff
changeset
|
104 %! assert (b(ib,:), c); |
9115
ef95b930f1cf
Add test for bugfix b2459d21a207
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
9108
diff
changeset
|
105 %!test |
ef95b930f1cf
Add test for bugfix b2459d21a207
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
9108
diff
changeset
|
106 %! a = [1 1 1 2 2 2]; |
ef95b930f1cf
Add test for bugfix b2459d21a207
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
9108
diff
changeset
|
107 %! b = [1 2 3 4 5 6]; |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14148
diff
changeset
|
108 %! c = intersect (a, b); |
9115
ef95b930f1cf
Add test for bugfix b2459d21a207
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
9108
diff
changeset
|
109 %! assert(c, [1,2]); |
14148
402acc45350e
intersect.m: Properly handle the "rows" case with more than 1 output arg (bug #35247)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
110 %!test |
402acc45350e
intersect.m: Properly handle the "rows" case with more than 1 output arg (bug #35247)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
111 %! a = [1 2 3 4; 5 6 7 8; 9 10 11 12]; |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14148
diff
changeset
|
112 %! [b, ia, ib] = intersect (a, a, "rows"); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14148
diff
changeset
|
113 %! assert (b, a); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14148
diff
changeset
|
114 %! assert (ia, [1:3]'); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14148
diff
changeset
|
115 %! assert (ib, [1:3]'); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14148
diff
changeset
|
116 |