annotate scripts/set/intersect.m @ 20475:3fc21d7ac11c

intersect.m: Return same class of output as Matlab for empty input (bug #45686). * intersect.m: For an empty input, return cell, char, or matrix based on Matlab rules. Add BIST tests for behavior.
author Rik <rik@octave.org>
date Tue, 11 Aug 2015 13:24:55 -0700
parents c3b2ec6a1586
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
19731
4197fc428c7d maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents: 19037
diff changeset
1 ## Copyright (C) 2000-2015 Paul Kienzle
11523
fd0a3ac60b0e update copyright notices
John W. Eaton <jwe@octave.org>
parents: 10793
diff changeset
2 ## Copyright (C) 2008-2009 Jaroslav Hajek
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 -*-
19037
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 17744
diff changeset
21 ## @deftypefn {Function File} {@var{c} =} intersect (@var{a}, @var{b})
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 17744
diff changeset
22 ## @deftypefnx {Function File} {@var{c} =} intersect (@var{a}, @var{b}, "rows")
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 17744
diff changeset
23 ## @deftypefnx {Function File} {[@var{c}, @var{ia}, @var{ib}] =} intersect (@dots{})
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 17744
diff changeset
24 ##
20009
72ccbd36e23c Return a column vector by default for Matlab compatibility (bug #44425, bug #44453).
Rik <rik@octave.org>
parents: 19731
diff changeset
25 ## Return the unique elements common to both @var{a} and @var{b} sorted in
72ccbd36e23c Return a column vector by default for Matlab compatibility (bug #44425, bug #44453).
Rik <rik@octave.org>
parents: 19731
diff changeset
26 ## ascending order.
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
27 ##
20009
72ccbd36e23c Return a column vector by default for Matlab compatibility (bug #44425, bug #44453).
Rik <rik@octave.org>
parents: 19731
diff changeset
28 ## If @var{a} and @var{b} are both row vectors then return a row vector;
72ccbd36e23c Return a column vector by default for Matlab compatibility (bug #44425, bug #44453).
Rik <rik@octave.org>
parents: 19731
diff changeset
29 ## Otherwise, return a column vector. The inputs may also be cell arrays of
19037
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 17744
diff changeset
30 ## strings.
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
31 ##
19037
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 17744
diff changeset
32 ## If the optional input @qcode{"rows"} is given then return the common rows of
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 17744
diff changeset
33 ## @var{a} and @var{b}. The inputs must be 2-D matrices to use this option.
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 17744
diff changeset
34 ##
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 17744
diff changeset
35 ## If requested, return index vectors @var{ia} and @var{ib} such that
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 17744
diff changeset
36 ## @code{@var{c} = @var{a}(@var{ia})} and @code{@var{c} = @var{b}(@var{ib})}.
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
37 ##
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
38 ## @end deftypefn
19037
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 17744
diff changeset
39 ## @seealso{unique, union, setdiff, setxor, ismember}
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
40
7920
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
41 function [c, ia, ib] = intersect (a, b, varargin)
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
42
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
43 if (nargin < 2 || nargin > 3)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5821
diff changeset
44 print_usage ();
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
45 endif
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
46
19037
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 17744
diff changeset
47 [a, b] = validsetargs ("intersect", a, b, varargin{:});
7920
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
48
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
49 if (isempty (a) || isempty (b))
20475
3fc21d7ac11c intersect.m: Return same class of output as Matlab for empty input (bug #45686).
Rik <rik@octave.org>
parents: 20472
diff changeset
50 ## Special case shortcuts algorithm.
3fc21d7ac11c intersect.m: Return same class of output as Matlab for empty input (bug #45686).
Rik <rik@octave.org>
parents: 20472
diff changeset
51 ## Lots of type checking required for Matlab compatibility.
3fc21d7ac11c intersect.m: Return same class of output as Matlab for empty input (bug #45686).
Rik <rik@octave.org>
parents: 20472
diff changeset
52 if (isnumeric (a) && isnumeric (b))
3fc21d7ac11c intersect.m: Return same class of output as Matlab for empty input (bug #45686).
Rik <rik@octave.org>
parents: 20472
diff changeset
53 c = [];
3fc21d7ac11c intersect.m: Return same class of output as Matlab for empty input (bug #45686).
Rik <rik@octave.org>
parents: 20472
diff changeset
54 elseif (iscell (b))
3fc21d7ac11c intersect.m: Return same class of output as Matlab for empty input (bug #45686).
Rik <rik@octave.org>
parents: 20472
diff changeset
55 c = {};
3fc21d7ac11c intersect.m: Return same class of output as Matlab for empty input (bug #45686).
Rik <rik@octave.org>
parents: 20472
diff changeset
56 else
3fc21d7ac11c intersect.m: Return same class of output as Matlab for empty input (bug #45686).
Rik <rik@octave.org>
parents: 20472
diff changeset
57 c = "";
3fc21d7ac11c intersect.m: Return same class of output as Matlab for empty input (bug #45686).
Rik <rik@octave.org>
parents: 20472
diff changeset
58 endif
3fc21d7ac11c intersect.m: Return same class of output as Matlab for empty input (bug #45686).
Rik <rik@octave.org>
parents: 20472
diff changeset
59 ia = ib = [];
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
60 else
19037
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 17744
diff changeset
61 by_rows = nargin == 3;
20472
c3b2ec6a1586 Remove redundant isvector() calls in set/ functions.
Rik <rik@octave.org>
parents: 20009
diff changeset
62 isrowvec = isrow (a) && isrow (b);
19037
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 17744
diff changeset
63
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 17744
diff changeset
64 ## Form A and B into sets
7920
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
65 if (nargout > 1)
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
66 [a, ja] = unique (a, varargin{:});
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
67 [b, jb] = unique (b, varargin{:});
9108
b2459d21a207 fix intersect bug
Jaroslav Hajek <highegg@gmail.com>
parents: 9051
diff changeset
68 else
b2459d21a207 fix intersect bug
Jaroslav Hajek <highegg@gmail.com>
parents: 9051
diff changeset
69 a = unique (a, varargin{:});
b2459d21a207 fix intersect bug
Jaroslav Hajek <highegg@gmail.com>
parents: 9051
diff changeset
70 b = unique (b, varargin{:});
6825
59e22e30aff8 [project @ 2007-08-24 15:36:49 by jwe]
jwe
parents: 6248
diff changeset
71 endif
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
72
19037
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 17744
diff changeset
73 if (by_rows)
7920
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
74 c = [a; b];
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
75 [c, ic] = sortrows (c);
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
76 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
77 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
78 len_a = rows (a);
7920
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
79 else
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
80 c = [a(:); b(:)];
19037
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 17744
diff changeset
81 [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
82 if (iscellstr (c))
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 10088
diff changeset
83 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
84 else
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 10088
diff changeset
85 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
86 endif
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
87 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
88 len_a = length (a);
7920
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
89 endif
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
90
7920
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
91 if (nargout > 1)
19037
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 17744
diff changeset
92 ia = ja(ic(ii)); # a(ia) == c
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 17744
diff changeset
93 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
94 endif
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
95
19037
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 17744
diff changeset
96 ## Adjust output orientation for Matlab compatibility
20009
72ccbd36e23c Return a column vector by default for Matlab compatibility (bug #44425, bug #44453).
Rik <rik@octave.org>
parents: 19731
diff changeset
97 if (! by_rows && isrowvec)
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
98 c = c.';
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
99 endif
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
100 endif
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
101
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
102 endfunction
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
103
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
104
19037
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 17744
diff changeset
105 ## Test orientation of output
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 17744
diff changeset
106 %!shared a,b
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 17744
diff changeset
107 %! a = 1:4;
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 17744
diff changeset
108 %! b = 2:5;
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 17744
diff changeset
109
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 17744
diff changeset
110 %!assert (size (intersect (a, b)), [1 3])
20009
72ccbd36e23c Return a column vector by default for Matlab compatibility (bug #44425, bug #44453).
Rik <rik@octave.org>
parents: 19731
diff changeset
111 %!assert (size (intersect (a', b)), [3 1])
72ccbd36e23c Return a column vector by default for Matlab compatibility (bug #44425, bug #44453).
Rik <rik@octave.org>
parents: 19731
diff changeset
112 %!assert (size (intersect (a, b')), [3 1])
19037
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 17744
diff changeset
113 %!assert (size (intersect (a', b')), [3 1])
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 17744
diff changeset
114
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 17744
diff changeset
115 ## Test multi-dimensional arrays
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 17744
diff changeset
116 %!test
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 17744
diff changeset
117 %! a = rand (3,3,3);
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 17744
diff changeset
118 %! b = a;
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 17744
diff changeset
119 %! b(1,1,1) = 2;
20009
72ccbd36e23c Return a column vector by default for Matlab compatibility (bug #44425, bug #44453).
Rik <rik@octave.org>
parents: 19731
diff changeset
120 %! assert (intersect (a, b), sort (a(2:end)'));
19037
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 17744
diff changeset
121
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 17744
diff changeset
122 ## Test the routine for index vectors ia and ib
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
123 %!test
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
124 %! 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
125 %! 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
126 %! [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
127 %! 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
128 %! 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
129 %! 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
130 %! assert (a(ia), c);
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14148
diff changeset
131 %! assert (b(ib), c);
7920
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
132 %!test
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
133 %! 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
134 %! 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
135 %! [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
136 %! 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
137 %! assert (ia, [1;2]);
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14148
diff changeset
138 %! assert (ib, [3;1]);
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14148
diff changeset
139 %! assert (a(ia,:), c);
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14148
diff changeset
140 %! assert (b(ib,:), c);
9115
ef95b930f1cf Add test for bugfix b2459d21a207
Thomas Weber <thomas.weber.mail@gmail.com>
parents: 9108
diff changeset
141 %!test
ef95b930f1cf Add test for bugfix b2459d21a207
Thomas Weber <thomas.weber.mail@gmail.com>
parents: 9108
diff changeset
142 %! a = [1 1 1 2 2 2];
ef95b930f1cf Add test for bugfix b2459d21a207
Thomas Weber <thomas.weber.mail@gmail.com>
parents: 9108
diff changeset
143 %! 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
144 %! c = intersect (a, b);
9115
ef95b930f1cf Add test for bugfix b2459d21a207
Thomas Weber <thomas.weber.mail@gmail.com>
parents: 9108
diff changeset
145 %! 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
146 %!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
147 %! 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
148 %! [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
149 %! assert (b, a);
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14148
diff changeset
150 %! assert (ia, [1:3]');
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14148
diff changeset
151 %! assert (ib, [1:3]');
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14148
diff changeset
152
20475
3fc21d7ac11c intersect.m: Return same class of output as Matlab for empty input (bug #45686).
Rik <rik@octave.org>
parents: 20472
diff changeset
153 ## Test return type of empty intersections
3fc21d7ac11c intersect.m: Return same class of output as Matlab for empty input (bug #45686).
Rik <rik@octave.org>
parents: 20472
diff changeset
154 %!assert (intersect (['a', 'b'], {}), {})
3fc21d7ac11c intersect.m: Return same class of output as Matlab for empty input (bug #45686).
Rik <rik@octave.org>
parents: 20472
diff changeset
155 %!assert (intersect ([], {'a', 'b'}), {})
3fc21d7ac11c intersect.m: Return same class of output as Matlab for empty input (bug #45686).
Rik <rik@octave.org>
parents: 20472
diff changeset
156 %!assert (intersect ([], {}), {})
3fc21d7ac11c intersect.m: Return same class of output as Matlab for empty input (bug #45686).
Rik <rik@octave.org>
parents: 20472
diff changeset
157 %!assert (intersect ({'a', 'b'}, []), {})
3fc21d7ac11c intersect.m: Return same class of output as Matlab for empty input (bug #45686).
Rik <rik@octave.org>
parents: 20472
diff changeset
158 %!assert (intersect ([], ['a', 'b']), "")
3fc21d7ac11c intersect.m: Return same class of output as Matlab for empty input (bug #45686).
Rik <rik@octave.org>
parents: 20472
diff changeset
159 %!assert (intersect ({}, []), {})
3fc21d7ac11c intersect.m: Return same class of output as Matlab for empty input (bug #45686).
Rik <rik@octave.org>
parents: 20472
diff changeset
160 %!assert (intersect (['a', 'b'], []), "")