annotate scripts/set/union.m @ 27919:1891570abac8

update Octave Project Developers copyright for the new year In files that have the "Octave Project Developers" copyright notice, update for 2020.
author John W. Eaton <jwe@octave.org>
date Mon, 06 Jan 2020 22:29:51 -0500
parents b442ec6dda5c
children bd51beb6205e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27919
1891570abac8 update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents: 27918
diff changeset
1 ## Copyright (C) 1994-2020 The Octave Project Developers
27918
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 27898
diff changeset
2 ##
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 27898
diff changeset
3 ## See the file COPYRIGHT.md in the top-level directory of this distribution
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 27898
diff changeset
4 ## or <https://octave.org/COPYRIGHT.html/>.
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 27898
diff changeset
5 ##
2313
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
6 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
7 ## This file is part of Octave.
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
8 ##
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23220
diff changeset
9 ## Octave is free software: you can redistribute it and/or modify it
2313
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
10 ## under the terms of the GNU General Public License as published by
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23220
diff changeset
11 ## the Free Software Foundation, either version 3 of the License, or
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22414
diff changeset
12 ## (at your option) any later version.
2313
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
13 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
14 ## Octave is distributed in the hope that it will be useful, but
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
15 ## WITHOUT ANY WARRANTY; without even the implied warranty of
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22414
diff changeset
16 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22414
diff changeset
17 ## GNU General Public License for more details.
2313
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
18 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
19 ## 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: 6046
diff changeset
20 ## along with Octave; see the file COPYING. If not, see
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23220
diff changeset
21 ## <https://www.gnu.org/licenses/>.
2303
5cffc4b8de57 [project @ 1996-06-24 09:15:24 by jwe]
jwe
parents: 904
diff changeset
22
3368
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 2847
diff changeset
23 ## -*- texinfo -*-
20852
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20438
diff changeset
24 ## @deftypefn {} {@var{c} =} union (@var{a}, @var{b})
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20438
diff changeset
25 ## @deftypefnx {} {@var{c} =} union (@var{a}, @var{b}, "rows")
27292
e2ba5f061806 union: Add "stable" sorting algorithm.
Rik <rik@octave.org>
parents: 27245
diff changeset
26 ## @deftypefnx {} {@var{c} =} union (@dots{}, "sorted")
e2ba5f061806 union: Add "stable" sorting algorithm.
Rik <rik@octave.org>
parents: 27245
diff changeset
27 ## @deftypefnx {} {@var{c} =} union (@dots{}, "stable")
27235
e12571df6466 doc: Update description of "legacy" option for set functions.
Rik <rik@octave.org>
parents: 27232
diff changeset
28 ## @deftypefnx {} {@var{c} =} union (@dots{}, "legacy")
20852
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20438
diff changeset
29 ## @deftypefnx {} {[@var{c}, @var{ia}, @var{ib}] =} union (@dots{})
16489
36dba9be680b doc: Make documentation compatible with Texinfo 5.0 (bug #38392)
Amod Mulay <amodmulay1@gmail.com>
parents: 14872
diff changeset
30 ##
27292
e2ba5f061806 union: Add "stable" sorting algorithm.
Rik <rik@octave.org>
parents: 27245
diff changeset
31 ## Return the unique elements that are in either @var{a} or @var{b}.
7344
4571f691b0ce [project @ 2008-01-04 18:18:22 by jwe]
jwe
parents: 7017
diff changeset
32 ##
19974
72ccbd36e23c Return a column vector by default for Matlab compatibility (bug #44425, bug #44453).
Rik <rik@octave.org>
parents: 19833
diff changeset
33 ## 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: 19833
diff changeset
34 ## Otherwise, return a column vector. The inputs may also be cell arrays of
19003
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 17744
diff changeset
35 ## strings.
10821
693e22af08ae Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
36 ##
19003
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 17744
diff changeset
37 ## If the optional input @qcode{"rows"} is given then return rows that are in
27292
e2ba5f061806 union: Add "stable" sorting algorithm.
Rik <rik@octave.org>
parents: 27245
diff changeset
38 ## either @var{a} or @var{b}. The inputs must be 2-D numeric matrices to use
e2ba5f061806 union: Add "stable" sorting algorithm.
Rik <rik@octave.org>
parents: 27245
diff changeset
39 ## this option.
e2ba5f061806 union: Add "stable" sorting algorithm.
Rik <rik@octave.org>
parents: 27245
diff changeset
40 ##
e2ba5f061806 union: Add "stable" sorting algorithm.
Rik <rik@octave.org>
parents: 27245
diff changeset
41 ## The optional argument @qcode{"sorted"}/@qcode{"stable"} controls the order
e2ba5f061806 union: Add "stable" sorting algorithm.
Rik <rik@octave.org>
parents: 27245
diff changeset
42 ## in which unique values appear in the output. The default is
e2ba5f061806 union: Add "stable" sorting algorithm.
Rik <rik@octave.org>
parents: 27245
diff changeset
43 ## @qcode{"sorted"} and values in the output are placed in ascending order.
e2ba5f061806 union: Add "stable" sorting algorithm.
Rik <rik@octave.org>
parents: 27245
diff changeset
44 ## The alternative @qcode{"stable"} preserves the order found in the input.
19597
db92e7e28e1f strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 19003
diff changeset
45 ##
27232
3d10834979f8 union.m: Accept a "legacy" flag for Matlab compatibility.
Rik <rik@octave.org>
parents: 27226
diff changeset
46 ## The optional outputs @var{ia} and @var{ib} are column index vectors such
3d10834979f8 union.m: Accept a "legacy" flag for Matlab compatibility.
Rik <rik@octave.org>
parents: 27226
diff changeset
47 ## that @code{@var{a}(@var{ia})} and @code{@var{b}(@var{ib})} are disjoint sets
19003
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 17744
diff changeset
48 ## whose union is @var{c}.
7920
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7411
diff changeset
49 ##
27235
e12571df6466 doc: Update description of "legacy" option for set functions.
Rik <rik@octave.org>
parents: 27232
diff changeset
50 ## Programming Note: The input flag @qcode{"legacy"} changes the algorithm
e12571df6466 doc: Update description of "legacy" option for set functions.
Rik <rik@octave.org>
parents: 27232
diff changeset
51 ## to be compatible with @sc{matlab} releases prior to R2012b.
e12571df6466 doc: Update description of "legacy" option for set functions.
Rik <rik@octave.org>
parents: 27232
diff changeset
52 ##
19003
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 17744
diff changeset
53 ## @seealso{unique, intersect, setdiff, setxor, ismember}
3406
772cc0f88f09 [project @ 2000-01-05 20:51:46 by jwe]
jwe
parents: 3405
diff changeset
54 ## @end deftypefn
3368
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 2847
diff changeset
55
7920
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7411
diff changeset
56 function [y, ia, ib] = union (a, b, varargin)
559
4e826edfbc56 [project @ 1994-07-25 22:18:28 by jwe]
jwe
parents:
diff changeset
57
27232
3d10834979f8 union.m: Accept a "legacy" flag for Matlab compatibility.
Rik <rik@octave.org>
parents: 27226
diff changeset
58 if (nargin < 2 || nargin > 4)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5642
diff changeset
59 print_usage ();
559
4e826edfbc56 [project @ 1994-07-25 22:18:28 by jwe]
jwe
parents:
diff changeset
60 endif
4e826edfbc56 [project @ 1994-07-25 22:18:28 by jwe]
jwe
parents:
diff changeset
61
19003
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 17744
diff changeset
62 [a, b] = validsetargs ("union", a, b, varargin{:});
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 17744
diff changeset
63
27232
3d10834979f8 union.m: Accept a "legacy" flag for Matlab compatibility.
Rik <rik@octave.org>
parents: 27226
diff changeset
64 by_rows = any (strcmp ("rows", varargin));
3d10834979f8 union.m: Accept a "legacy" flag for Matlab compatibility.
Rik <rik@octave.org>
parents: 27226
diff changeset
65 optlegacy = any (strcmp ("legacy", varargin));
3d10834979f8 union.m: Accept a "legacy" flag for Matlab compatibility.
Rik <rik@octave.org>
parents: 27226
diff changeset
66
3d10834979f8 union.m: Accept a "legacy" flag for Matlab compatibility.
Rik <rik@octave.org>
parents: 27226
diff changeset
67 if (optlegacy)
3d10834979f8 union.m: Accept a "legacy" flag for Matlab compatibility.
Rik <rik@octave.org>
parents: 27226
diff changeset
68 isrowvec = ! iscolumn (a) || ! iscolumn (b);
3d10834979f8 union.m: Accept a "legacy" flag for Matlab compatibility.
Rik <rik@octave.org>
parents: 27226
diff changeset
69 else
3d10834979f8 union.m: Accept a "legacy" flag for Matlab compatibility.
Rik <rik@octave.org>
parents: 27226
diff changeset
70 isrowvec = isrow (a) && isrow (b);
3d10834979f8 union.m: Accept a "legacy" flag for Matlab compatibility.
Rik <rik@octave.org>
parents: 27226
diff changeset
71 endif
7344
4571f691b0ce [project @ 2008-01-04 18:18:22 by jwe]
jwe
parents: 7017
diff changeset
72
19003
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 17744
diff changeset
73 if (by_rows)
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 17744
diff changeset
74 y = [a; b];
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 17744
diff changeset
75 else
7920
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7411
diff changeset
76 y = [a(:); b(:)];
19003
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 17744
diff changeset
77 ## Adjust output orientation for Matlab compatibility
19974
72ccbd36e23c Return a column vector by default for Matlab compatibility (bug #44425, bug #44453).
Rik <rik@octave.org>
parents: 19833
diff changeset
78 if (isrowvec)
7920
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7411
diff changeset
79 y = y.';
4317
1ced186d4b6b [project @ 2003-02-11 16:10:09 by jwe]
jwe
parents: 3426
diff changeset
80 endif
559
4e826edfbc56 [project @ 1994-07-25 22:18:28 by jwe]
jwe
parents:
diff changeset
81 endif
4e826edfbc56 [project @ 1994-07-25 22:18:28 by jwe]
jwe
parents:
diff changeset
82
19003
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 17744
diff changeset
83 if (nargout <= 1)
7920
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7411
diff changeset
84 y = unique (y, varargin{:});
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7411
diff changeset
85 else
19003
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 17744
diff changeset
86 [y, idx] = unique (y, varargin{:});
22414
5a3f581a314f union.m: output proper indices for 'rows' inputs (bug #48857).
Joakim Andén <janden@math.princeton.edu>
parents: 22323
diff changeset
87 if (by_rows)
5a3f581a314f union.m: output proper indices for 'rows' inputs (bug #48857).
Joakim Andén <janden@math.princeton.edu>
parents: 22323
diff changeset
88 na = rows (a);
5a3f581a314f union.m: output proper indices for 'rows' inputs (bug #48857).
Joakim Andén <janden@math.princeton.edu>
parents: 22323
diff changeset
89 else
5a3f581a314f union.m: output proper indices for 'rows' inputs (bug #48857).
Joakim Andén <janden@math.princeton.edu>
parents: 22323
diff changeset
90 na = numel (a);
5a3f581a314f union.m: output proper indices for 'rows' inputs (bug #48857).
Joakim Andén <janden@math.princeton.edu>
parents: 22323
diff changeset
91 end
19003
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 17744
diff changeset
92 ia = idx(idx <= na);
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 17744
diff changeset
93 ib = idx(idx > na) - na;
7920
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7411
diff changeset
94 endif
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7411
diff changeset
95
559
4e826edfbc56 [project @ 1994-07-25 22:18:28 by jwe]
jwe
parents:
diff changeset
96 endfunction
7411
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7344
diff changeset
97
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7344
diff changeset
98
19003
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 17744
diff changeset
99 %!assert (union ([1, 2, 4], [2, 3, 5]), [1, 2, 3, 4, 5])
19974
72ccbd36e23c Return a column vector by default for Matlab compatibility (bug #44425, bug #44453).
Rik <rik@octave.org>
parents: 19833
diff changeset
100 %!assert (union ([1; 2; 4], [2, 3, 5]), [1; 2; 3; 4; 5])
19003
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 17744
diff changeset
101 %!assert (union ([1; 2; 4], [2; 3; 5]), [1; 2; 3; 4; 5])
19974
72ccbd36e23c Return a column vector by default for Matlab compatibility (bug #44425, bug #44453).
Rik <rik@octave.org>
parents: 19833
diff changeset
102 %!assert (union ([1, 2, 3], [5; 7; 9]), [1; 2; 3; 5; 7; 9])
22414
5a3f581a314f union.m: output proper indices for 'rows' inputs (bug #48857).
Joakim Andén <janden@math.princeton.edu>
parents: 22323
diff changeset
103 %!assert (union ([1 2; 2 3; 4 5], [2 3; 3 4; 5 6], "rows"),
5a3f581a314f union.m: output proper indices for 'rows' inputs (bug #48857).
Joakim Andén <janden@math.princeton.edu>
parents: 22323
diff changeset
104 %! [1 2; 2 3; 3 4; 4 5; 5 6])
19003
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 17744
diff changeset
105
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 17744
diff changeset
106 ## Test multi-dimensional arrays
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 17744
diff changeset
107 %!test
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 17744
diff changeset
108 %! a = rand (3,3,3);
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 17744
diff changeset
109 %! b = a;
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 17744
diff changeset
110 %! b(1,1,1) = 2;
19974
72ccbd36e23c Return a column vector by default for Matlab compatibility (bug #44425, bug #44453).
Rik <rik@octave.org>
parents: 19833
diff changeset
111 %! assert (union (a, b), sort ([a(1:end)'; 2]));
7411
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7344
diff changeset
112
7920
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7411
diff changeset
113 %!test
19597
db92e7e28e1f strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 19003
diff changeset
114 %! a = [3, 1, 4, 1, 5];
27292
e2ba5f061806 union: Add "stable" sorting algorithm.
Rik <rik@octave.org>
parents: 27245
diff changeset
115 %! b = [1; 2; 3; 4];
e2ba5f061806 union: Add "stable" sorting algorithm.
Rik <rik@octave.org>
parents: 27245
diff changeset
116 %! [y, ia, ib] = union (a, b);
19974
72ccbd36e23c Return a column vector by default for Matlab compatibility (bug #44425, bug #44453).
Rik <rik@octave.org>
parents: 19833
diff changeset
117 %! assert (y, [1; 2; 3; 4; 5]);
72ccbd36e23c Return a column vector by default for Matlab compatibility (bug #44425, bug #44453).
Rik <rik@octave.org>
parents: 19833
diff changeset
118 %! assert (y, sort ([a(ia)'; b(ib)']));
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14327
diff changeset
119
27292
e2ba5f061806 union: Add "stable" sorting algorithm.
Rik <rik@octave.org>
parents: 27245
diff changeset
120 ## Test "stable" sorting order
e2ba5f061806 union: Add "stable" sorting algorithm.
Rik <rik@octave.org>
parents: 27245
diff changeset
121 %!assert (union ([1, 2, 4], [2, 3, 5], "stable"), [1, 2, 4, 3, 5])
e2ba5f061806 union: Add "stable" sorting algorithm.
Rik <rik@octave.org>
parents: 27245
diff changeset
122 %!assert (union ([1, 2, 4]', [2, 3, 5], "stable"), [1; 2; 4; 3; 5])
e2ba5f061806 union: Add "stable" sorting algorithm.
Rik <rik@octave.org>
parents: 27245
diff changeset
123 %!assert (union ([1, 2, 4], [2, 3, 5]', "stable"), [1; 2; 4; 3; 5])
e2ba5f061806 union: Add "stable" sorting algorithm.
Rik <rik@octave.org>
parents: 27245
diff changeset
124
e2ba5f061806 union: Add "stable" sorting algorithm.
Rik <rik@octave.org>
parents: 27245
diff changeset
125 %!test
e2ba5f061806 union: Add "stable" sorting algorithm.
Rik <rik@octave.org>
parents: 27245
diff changeset
126 %! a = [3, 1, 4, 1, 5];
e2ba5f061806 union: Add "stable" sorting algorithm.
Rik <rik@octave.org>
parents: 27245
diff changeset
127 %! b = [1; 2; 3; 4];
e2ba5f061806 union: Add "stable" sorting algorithm.
Rik <rik@octave.org>
parents: 27245
diff changeset
128 %! [y, ia, ib] = union (a, b, "stable");
e2ba5f061806 union: Add "stable" sorting algorithm.
Rik <rik@octave.org>
parents: 27245
diff changeset
129 %! assert (y, [3; 1; 4; 5; 2]);
e2ba5f061806 union: Add "stable" sorting algorithm.
Rik <rik@octave.org>
parents: 27245
diff changeset
130 %! assert (ia, [1; 2; 3; 5]);
e2ba5f061806 union: Add "stable" sorting algorithm.
Rik <rik@octave.org>
parents: 27245
diff changeset
131 %! assert (ib, [2]);
e2ba5f061806 union: Add "stable" sorting algorithm.
Rik <rik@octave.org>
parents: 27245
diff changeset
132
e2ba5f061806 union: Add "stable" sorting algorithm.
Rik <rik@octave.org>
parents: 27245
diff changeset
133 ## Test indexing outputs
e2ba5f061806 union: Add "stable" sorting algorithm.
Rik <rik@octave.org>
parents: 27245
diff changeset
134 %!test
e2ba5f061806 union: Add "stable" sorting algorithm.
Rik <rik@octave.org>
parents: 27245
diff changeset
135 %! a = [1, 4, 2];
e2ba5f061806 union: Add "stable" sorting algorithm.
Rik <rik@octave.org>
parents: 27245
diff changeset
136 %! b = [2, 3, 5];
e2ba5f061806 union: Add "stable" sorting algorithm.
Rik <rik@octave.org>
parents: 27245
diff changeset
137 %! [~, ia, ib] = union (a, b);
e2ba5f061806 union: Add "stable" sorting algorithm.
Rik <rik@octave.org>
parents: 27245
diff changeset
138 %! assert (ia, [1; 3; 2]);
e2ba5f061806 union: Add "stable" sorting algorithm.
Rik <rik@octave.org>
parents: 27245
diff changeset
139 %! assert (ib, [2; 3]);
e2ba5f061806 union: Add "stable" sorting algorithm.
Rik <rik@octave.org>
parents: 27245
diff changeset
140 %! [~, ia, ib] = union (a, b, "stable");
e2ba5f061806 union: Add "stable" sorting algorithm.
Rik <rik@octave.org>
parents: 27245
diff changeset
141 %! assert (ia, [1; 2; 3]);
e2ba5f061806 union: Add "stable" sorting algorithm.
Rik <rik@octave.org>
parents: 27245
diff changeset
142 %! assert (ib, [2; 3]);
e2ba5f061806 union: Add "stable" sorting algorithm.
Rik <rik@octave.org>
parents: 27245
diff changeset
143
e2ba5f061806 union: Add "stable" sorting algorithm.
Rik <rik@octave.org>
parents: 27245
diff changeset
144 %!test
e2ba5f061806 union: Add "stable" sorting algorithm.
Rik <rik@octave.org>
parents: 27245
diff changeset
145 %! a = [1 2; 4 5; 2 3];
e2ba5f061806 union: Add "stable" sorting algorithm.
Rik <rik@octave.org>
parents: 27245
diff changeset
146 %! b = [2 3; 3 4; 5 6];
e2ba5f061806 union: Add "stable" sorting algorithm.
Rik <rik@octave.org>
parents: 27245
diff changeset
147 %! [~, ia, ib] = union (a, b, "rows");
e2ba5f061806 union: Add "stable" sorting algorithm.
Rik <rik@octave.org>
parents: 27245
diff changeset
148 %! assert (ia, [1; 3; 2]);
e2ba5f061806 union: Add "stable" sorting algorithm.
Rik <rik@octave.org>
parents: 27245
diff changeset
149 %! assert ([2; 3]);
e2ba5f061806 union: Add "stable" sorting algorithm.
Rik <rik@octave.org>
parents: 27245
diff changeset
150 %! [~, ia, ib] = union (a, b, "rows", "stable");
e2ba5f061806 union: Add "stable" sorting algorithm.
Rik <rik@octave.org>
parents: 27245
diff changeset
151 %! assert (ia, [1; 2; 3]);
e2ba5f061806 union: Add "stable" sorting algorithm.
Rik <rik@octave.org>
parents: 27245
diff changeset
152 %! assert ([2; 3]);
22414
5a3f581a314f union.m: output proper indices for 'rows' inputs (bug #48857).
Joakim Andén <janden@math.princeton.edu>
parents: 22323
diff changeset
153
27232
3d10834979f8 union.m: Accept a "legacy" flag for Matlab compatibility.
Rik <rik@octave.org>
parents: 27226
diff changeset
154 ## Test "legacy" option
3d10834979f8 union.m: Accept a "legacy" flag for Matlab compatibility.
Rik <rik@octave.org>
parents: 27226
diff changeset
155 %!test
3d10834979f8 union.m: Accept a "legacy" flag for Matlab compatibility.
Rik <rik@octave.org>
parents: 27226
diff changeset
156 %! a = [5, 7, 1];
3d10834979f8 union.m: Accept a "legacy" flag for Matlab compatibility.
Rik <rik@octave.org>
parents: 27226
diff changeset
157 %! b = [3, 1, 1];
3d10834979f8 union.m: Accept a "legacy" flag for Matlab compatibility.
Rik <rik@octave.org>
parents: 27226
diff changeset
158 %! [c, ia, ib] = union (a,b);
3d10834979f8 union.m: Accept a "legacy" flag for Matlab compatibility.
Rik <rik@octave.org>
parents: 27226
diff changeset
159 %! assert (c, [1, 3, 5, 7]);
3d10834979f8 union.m: Accept a "legacy" flag for Matlab compatibility.
Rik <rik@octave.org>
parents: 27226
diff changeset
160 %! assert (ia, [3; 1; 2]);
3d10834979f8 union.m: Accept a "legacy" flag for Matlab compatibility.
Rik <rik@octave.org>
parents: 27226
diff changeset
161 %! assert (ib, [1]);
3d10834979f8 union.m: Accept a "legacy" flag for Matlab compatibility.
Rik <rik@octave.org>
parents: 27226
diff changeset
162 %! [c, ia, ib] = union (a,b, "legacy");
3d10834979f8 union.m: Accept a "legacy" flag for Matlab compatibility.
Rik <rik@octave.org>
parents: 27226
diff changeset
163 %! assert (c, [1, 3, 5, 7]);
3d10834979f8 union.m: Accept a "legacy" flag for Matlab compatibility.
Rik <rik@octave.org>
parents: 27226
diff changeset
164 %! assert (ia, [1, 2]);
3d10834979f8 union.m: Accept a "legacy" flag for Matlab compatibility.
Rik <rik@octave.org>
parents: 27226
diff changeset
165 %! assert (ib, [3, 1]);
3d10834979f8 union.m: Accept a "legacy" flag for Matlab compatibility.
Rik <rik@octave.org>
parents: 27226
diff changeset
166
27296
538468f901dd Clean up BIST tests in set functions.
Rik <rik@octave.org>
parents: 27292
diff changeset
167 ## Test orientation of output
538468f901dd Clean up BIST tests in set functions.
Rik <rik@octave.org>
parents: 27292
diff changeset
168 %!shared x,y
538468f901dd Clean up BIST tests in set functions.
Rik <rik@octave.org>
parents: 27292
diff changeset
169 %! x = 1:3;
538468f901dd Clean up BIST tests in set functions.
Rik <rik@octave.org>
parents: 27292
diff changeset
170 %! y = 2:5;
538468f901dd Clean up BIST tests in set functions.
Rik <rik@octave.org>
parents: 27292
diff changeset
171
538468f901dd Clean up BIST tests in set functions.
Rik <rik@octave.org>
parents: 27292
diff changeset
172 %!assert (size (union (x, y)), [1 5])
538468f901dd Clean up BIST tests in set functions.
Rik <rik@octave.org>
parents: 27292
diff changeset
173 %!assert (size (union (x', y)), [5 1])
538468f901dd Clean up BIST tests in set functions.
Rik <rik@octave.org>
parents: 27292
diff changeset
174 %!assert (size (union (x, y')), [5 1])
538468f901dd Clean up BIST tests in set functions.
Rik <rik@octave.org>
parents: 27292
diff changeset
175 %!assert (size (union (x', y')), [5 1])
538468f901dd Clean up BIST tests in set functions.
Rik <rik@octave.org>
parents: 27292
diff changeset
176
538468f901dd Clean up BIST tests in set functions.
Rik <rik@octave.org>
parents: 27292
diff changeset
177 ## Clear shared variables
538468f901dd Clean up BIST tests in set functions.
Rik <rik@octave.org>
parents: 27292
diff changeset
178 %!shared
538468f901dd Clean up BIST tests in set functions.
Rik <rik@octave.org>
parents: 27292
diff changeset
179
20438
e59a44fa74ff union.m: Matlab compatible output when mixed input and one is empty (bug #45685).
Juan Pablo Carbajal <ajuanpi+dev@gmail.com>
parents: 20437
diff changeset
180 ## Test empty cell string array unions
e59a44fa74ff union.m: Matlab compatible output when mixed input and one is empty (bug #45685).
Juan Pablo Carbajal <ajuanpi+dev@gmail.com>
parents: 20437
diff changeset
181 %!assert (union ({}, []), cell (0,1))
e59a44fa74ff union.m: Matlab compatible output when mixed input and one is empty (bug #45685).
Juan Pablo Carbajal <ajuanpi+dev@gmail.com>
parents: 20437
diff changeset
182 %!assert (union ([], {}), cell (0,1))
e59a44fa74ff union.m: Matlab compatible output when mixed input and one is empty (bug #45685).
Juan Pablo Carbajal <ajuanpi+dev@gmail.com>
parents: 20437
diff changeset
183 %!assert (union ([], {'a', 'b'}), {'a';'b'})
e59a44fa74ff union.m: Matlab compatible output when mixed input and one is empty (bug #45685).
Juan Pablo Carbajal <ajuanpi+dev@gmail.com>
parents: 20437
diff changeset
184 %!assert (union ({'a', 'b'}, []), {'a';'b'})
e59a44fa74ff union.m: Matlab compatible output when mixed input and one is empty (bug #45685).
Juan Pablo Carbajal <ajuanpi+dev@gmail.com>
parents: 20437
diff changeset
185 %!assert (union (['a', 'b'], {}), {'ab'})
e59a44fa74ff union.m: Matlab compatible output when mixed input and one is empty (bug #45685).
Juan Pablo Carbajal <ajuanpi+dev@gmail.com>
parents: 20437
diff changeset
186 %!assert (union ({}, ['a', 'b']), {'ab'})
e59a44fa74ff union.m: Matlab compatible output when mixed input and one is empty (bug #45685).
Juan Pablo Carbajal <ajuanpi+dev@gmail.com>
parents: 20437
diff changeset
187
19833
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19697
diff changeset
188 ## Test common input validation for set routines contained in validsetargs
19003
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 17744
diff changeset
189 %!error <cell array of strings cannot be combined> union ({"a"}, 1)
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 17744
diff changeset
190 %!error <A and B must be arrays or cell arrays> union (@sin, 1)
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 17744
diff changeset
191 %!error <cells not supported with "rows"> union ({"a"}, {"b"}, "rows")
27292
e2ba5f061806 union: Add "stable" sorting algorithm.
Rik <rik@octave.org>
parents: 27245
diff changeset
192 %!error <cells not supported with "rows"> union ({"a"}, {"b"}, "rows","legacy")
19003
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 17744
diff changeset
193 %!error <A and B must be arrays or cell arrays> union (@sin, 1, "rows")
27292
e2ba5f061806 union: Add "stable" sorting algorithm.
Rik <rik@octave.org>
parents: 27245
diff changeset
194 %!error <A and B must be arrays or cell arrays> union (@sin,1,"rows","legacy")
19003
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 17744
diff changeset
195 %!error <A and B must be 2-dimensional matrices> union (rand(2,2,2), 1, "rows")
27245
9ded07d2c44f validsetargs.m: Validate "rows" flag when "legacy" flag also present.
Rik <rik@octave.org>
parents: 27236
diff changeset
196 %!error <A and B must be 2-dimensional matrices> union (1, rand(2,2,2), "rows")
27292
e2ba5f061806 union: Add "stable" sorting algorithm.
Rik <rik@octave.org>
parents: 27245
diff changeset
197 %!error <A and B must be 2-dimensional matrices>
e2ba5f061806 union: Add "stable" sorting algorithm.
Rik <rik@octave.org>
parents: 27245
diff changeset
198 %! union (rand(2,2,2), 1, "rows", "legacy");
e2ba5f061806 union: Add "stable" sorting algorithm.
Rik <rik@octave.org>
parents: 27245
diff changeset
199 %!error <A and B must be 2-dimensional matrices>
e2ba5f061806 union: Add "stable" sorting algorithm.
Rik <rik@octave.org>
parents: 27245
diff changeset
200 %! union (1, rand(2,2,2), "rows", "legacy");
19003
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 17744
diff changeset
201 %!error <number of columns in A and B must match> union ([1 2], 1, "rows")
27245
9ded07d2c44f validsetargs.m: Validate "rows" flag when "legacy" flag also present.
Rik <rik@octave.org>
parents: 27236
diff changeset
202 %!error <number of columns in A and B must match> union (1, [1 2], "rows")
27292
e2ba5f061806 union: Add "stable" sorting algorithm.
Rik <rik@octave.org>
parents: 27245
diff changeset
203 %!error <number of columns in A and B must match>
e2ba5f061806 union: Add "stable" sorting algorithm.
Rik <rik@octave.org>
parents: 27245
diff changeset
204 %! union ([1 2], 1, "rows", "legacy");
e2ba5f061806 union: Add "stable" sorting algorithm.
Rik <rik@octave.org>
parents: 27245
diff changeset
205 %!error <number of columns in A and B must match>
e2ba5f061806 union: Add "stable" sorting algorithm.
Rik <rik@octave.org>
parents: 27245
diff changeset
206 %! union (1, [1 2], "rows", "legacy");
e2ba5f061806 union: Add "stable" sorting algorithm.
Rik <rik@octave.org>
parents: 27245
diff changeset
207 %!error <invalid option: columns> union (1, 2, "columns")
27245
9ded07d2c44f validsetargs.m: Validate "rows" flag when "legacy" flag also present.
Rik <rik@octave.org>
parents: 27236
diff changeset
208 %!error <invalid option: columns> union (1, 2, "legacy", "columns")
27292
e2ba5f061806 union: Add "stable" sorting algorithm.
Rik <rik@octave.org>
parents: 27245
diff changeset
209 %!error <only one of "sorted", "stable", or "legacy" may be specified>
e2ba5f061806 union: Add "stable" sorting algorithm.
Rik <rik@octave.org>
parents: 27245
diff changeset
210 %! union (1, 2, "sorted", "stable");
e2ba5f061806 union: Add "stable" sorting algorithm.
Rik <rik@octave.org>
parents: 27245
diff changeset
211 %!error <only one of "sorted", "stable", or "legacy" may be specified>
e2ba5f061806 union: Add "stable" sorting algorithm.
Rik <rik@octave.org>
parents: 27245
diff changeset
212 %! union (1, 2, "sorted", "legacy");