annotate scripts/set/unique.m @ 27236:d29a12e8b6d9

unique.m: Set default to "first" when multiple options present. * unique.m: Set optfirst variable if options present but neither "first" or "last" given. Use optfirst ordering unless "legacy" has been given. Change BIST tests to use variable name 'y' which is what function prototype uses for output argument. Write better BIST test for unique with "rows" AND "legacy" option present. * union.m: Correct BIST test for new behavior.
author Rik <rik@octave.org>
date Thu, 11 Jul 2019 15:05:30 -0700
parents e12571df6466
children c4f9a0f097a3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
26376
00f796120a6d maint: Update copyright dates in all source files.
John W. Eaton <jwe@octave.org>
parents: 25054
diff changeset
1 ## Copyright (C) 2000-2019 Paul Kienzle
11523
fd0a3ac60b0e update copyright notices
John W. Eaton <jwe@octave.org>
parents: 11097
diff changeset
2 ## Copyright (C) 2008-2009 Jaroslav Hajek
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6609
diff changeset
3 ##
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6609
diff changeset
4 ## This file is part of Octave.
5165
b822b4895af2 [project @ 2005-02-27 20:51:35 by dbateman]
dbateman
parents:
diff changeset
5 ##
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23220
diff changeset
6 ## Octave is free software: you can redistribute it and/or modify it
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6609
diff changeset
7 ## 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
8 ## the Free Software Foundation, either version 3 of the License, or
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
9 ## (at your option) any later version.
5165
b822b4895af2 [project @ 2005-02-27 20:51:35 by dbateman]
dbateman
parents:
diff changeset
10 ##
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6609
diff changeset
11 ## Octave is distributed in the hope that it will be useful, but
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6609
diff changeset
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
14 ## GNU General Public License for more details.
5165
b822b4895af2 [project @ 2005-02-27 20:51:35 by dbateman]
dbateman
parents:
diff changeset
15 ##
b822b4895af2 [project @ 2005-02-27 20:51:35 by dbateman]
dbateman
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: 6609
diff changeset
17 ## 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
18 ## <https://www.gnu.org/licenses/>.
5165
b822b4895af2 [project @ 2005-02-27 20:51:35 by dbateman]
dbateman
parents:
diff changeset
19
b822b4895af2 [project @ 2005-02-27 20:51:35 by dbateman]
dbateman
parents:
diff changeset
20 ## -*- texinfo -*-
20852
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 19833
diff changeset
21 ## @deftypefn {} {} unique (@var{x})
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 19833
diff changeset
22 ## @deftypefnx {} {} unique (@var{x}, "rows")
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 19833
diff changeset
23 ## @deftypefnx {} {[@var{y}, @var{i}, @var{j}] =} unique (@dots{})
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 19833
diff changeset
24 ## @deftypefnx {} {[@var{y}, @var{i}, @var{j}] =} unique (@dots{}, "first")
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 19833
diff changeset
25 ## @deftypefnx {} {[@var{y}, @var{i}, @var{j}] =} unique (@dots{}, "last")
27226
6eb32f0aea87 Implement "legacy" flag for intersect function (bug #56592).
Rik <rik@octave.org>
parents: 26376
diff changeset
26 ## @deftypefnx {} {[@var{y}, @var{i}, @var{j}] =} unique (@dots{}, "legacy")
19003
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18857
diff changeset
27 ## Return the unique elements of @var{x} sorted in ascending order.
5165
b822b4895af2 [project @ 2005-02-27 20:51:35 by dbateman]
dbateman
parents:
diff changeset
28 ##
19003
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18857
diff changeset
29 ## If the input @var{x} is a column vector then return a column vector;
21546
f7f97d7e9294 doc: Wrap m-file docstrings to 79 characters + newline (80 total).
Rik <rik@octave.org>
parents: 20955
diff changeset
30 ## Otherwise, return a row vector. @var{x} may also be a cell array of
f7f97d7e9294 doc: Wrap m-file docstrings to 79 characters + newline (80 total).
Rik <rik@octave.org>
parents: 20955
diff changeset
31 ## strings.
19003
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18857
diff changeset
32 ##
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18857
diff changeset
33 ## If the optional argument @qcode{"rows"} is given then return the unique
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18857
diff changeset
34 ## rows of @var{x} sorted in ascending order. The input must be a 2-D matrix
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18857
diff changeset
35 ## to use this option.
5165
b822b4895af2 [project @ 2005-02-27 20:51:35 by dbateman]
dbateman
parents:
diff changeset
36 ##
27226
6eb32f0aea87 Implement "legacy" flag for intersect function (bug #56592).
Rik <rik@octave.org>
parents: 26376
diff changeset
37 ## If requested, return column index vectors @var{i} and @var{j} such that
19003
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18857
diff changeset
38 ## @code{@var{y} = @var{x}(@var{i})} and @code{@var{x} = @var{y}(@var{j})}.
8887
6e4a811e58f8 deprecate create_set
John W. Eaton <jwe@octave.org>
parents: 8664
diff changeset
39 ##
27226
6eb32f0aea87 Implement "legacy" flag for intersect function (bug #56592).
Rik <rik@octave.org>
parents: 26376
diff changeset
40 ## Additionally, if @var{i} is a requested output then one of the flags
6eb32f0aea87 Implement "legacy" flag for intersect function (bug #56592).
Rik <rik@octave.org>
parents: 26376
diff changeset
41 ## @qcode{"first"} or @qcode{"last"} may be given. If @qcode{"last"} is
6eb32f0aea87 Implement "legacy" flag for intersect function (bug #56592).
Rik <rik@octave.org>
parents: 26376
diff changeset
42 ## specified, return the highest possible indices in @var{i}, otherwise, if
6eb32f0aea87 Implement "legacy" flag for intersect function (bug #56592).
Rik <rik@octave.org>
parents: 26376
diff changeset
43 ## @qcode{"first"} is specified, return the lowest. The default is
6eb32f0aea87 Implement "legacy" flag for intersect function (bug #56592).
Rik <rik@octave.org>
parents: 26376
diff changeset
44 ## @qcode{"first"}.
6eb32f0aea87 Implement "legacy" flag for intersect function (bug #56592).
Rik <rik@octave.org>
parents: 26376
diff changeset
45 ##
27235
e12571df6466 doc: Update description of "legacy" option for set functions.
Rik <rik@octave.org>
parents: 27228
diff changeset
46 ## 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: 27228
diff changeset
47 ## to be compatible with @sc{matlab} releases prior to R2012b. Specifically,
e12571df6466 doc: Update description of "legacy" option for set functions.
Rik <rik@octave.org>
parents: 27228
diff changeset
48 ## The index ordering flag is changed to @qcode{"last"}, and the shape of the
e12571df6466 doc: Update description of "legacy" option for set functions.
Rik <rik@octave.org>
parents: 27228
diff changeset
49 ## outputs @var{i}, @var{j} will follow the shape of the input @var{x} rather
e12571df6466 doc: Update description of "legacy" option for set functions.
Rik <rik@octave.org>
parents: 27228
diff changeset
50 ## than always being column vectors.
27226
6eb32f0aea87 Implement "legacy" flag for intersect function (bug #56592).
Rik <rik@octave.org>
parents: 26376
diff changeset
51 ##
5642
2618a0750ae6 [project @ 2006-03-06 21:26:48 by jwe]
jwe
parents: 5532
diff changeset
52 ## @seealso{union, intersect, setdiff, setxor, ismember}
5165
b822b4895af2 [project @ 2005-02-27 20:51:35 by dbateman]
dbateman
parents:
diff changeset
53 ## @end deftypefn
b822b4895af2 [project @ 2005-02-27 20:51:35 by dbateman]
dbateman
parents:
diff changeset
54
7920
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7650
diff changeset
55 function [y, i, j] = unique (x, varargin)
5165
b822b4895af2 [project @ 2005-02-27 20:51:35 by dbateman]
dbateman
parents:
diff changeset
56
7920
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7650
diff changeset
57 if (nargin < 1)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5737
diff changeset
58 print_usage ();
19700
00e31f316a3a Fix Matlab incompatibility of "ismatrix" (bug #42422).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 19697
diff changeset
59 elseif (! (isnumeric (x) || islogical (x) || ischar (x) || iscellstr (x)))
00e31f316a3a Fix Matlab incompatibility of "ismatrix" (bug #42422).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 19697
diff changeset
60 error ("unique: X must be an array or cell array of strings");
5165
b822b4895af2 [project @ 2005-02-27 20:51:35 by dbateman]
dbateman
parents:
diff changeset
61 endif
b822b4895af2 [project @ 2005-02-27 20:51:35 by dbateman]
dbateman
parents:
diff changeset
62
8412
970b4dbff9e4 optimize unique called with a single argument
Daniel J Sebald <daniel.sebald@ieee.org>
parents: 7920
diff changeset
63 if (nargin > 1)
970b4dbff9e4 optimize unique called with a single argument
Daniel J Sebald <daniel.sebald@ieee.org>
parents: 7920
diff changeset
64 ## parse options
19003
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18857
diff changeset
65 if (! iscellstr (varargin))
8412
970b4dbff9e4 optimize unique called with a single argument
Daniel J Sebald <daniel.sebald@ieee.org>
parents: 7920
diff changeset
66 error ("unique: options must be strings");
7920
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7650
diff changeset
67 endif
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7650
diff changeset
68
27226
6eb32f0aea87 Implement "legacy" flag for intersect function (bug #56592).
Rik <rik@octave.org>
parents: 26376
diff changeset
69 optrows = any (strcmp ("rows", varargin));
6eb32f0aea87 Implement "legacy" flag for intersect function (bug #56592).
Rik <rik@octave.org>
parents: 26376
diff changeset
70 optfirst = any (strcmp ("first", varargin));
6eb32f0aea87 Implement "legacy" flag for intersect function (bug #56592).
Rik <rik@octave.org>
parents: 26376
diff changeset
71 optlast = any (strcmp ("last", varargin));
6eb32f0aea87 Implement "legacy" flag for intersect function (bug #56592).
Rik <rik@octave.org>
parents: 26376
diff changeset
72 optlegacy = any (strcmp ("legacy", varargin));
19003
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18857
diff changeset
73 if (optfirst && optlast)
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18857
diff changeset
74 error ('unique: cannot specify both "first" and "last"');
27226
6eb32f0aea87 Implement "legacy" flag for intersect function (bug #56592).
Rik <rik@octave.org>
parents: 26376
diff changeset
75 elseif (optfirst + optlast + optrows + optlegacy != nargin-1)
19003
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18857
diff changeset
76 error ("unique: invalid option");
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18857
diff changeset
77 endif
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18857
diff changeset
78
27236
d29a12e8b6d9 unique.m: Set default to "first" when multiple options present.
Rik <rik@octave.org>
parents: 27235
diff changeset
79 ## Set default to "first" if not set earlier.
d29a12e8b6d9 unique.m: Set default to "first" when multiple options present.
Rik <rik@octave.org>
parents: 27235
diff changeset
80 if (! optfirst && ! optlast)
d29a12e8b6d9 unique.m: Set default to "first" when multiple options present.
Rik <rik@octave.org>
parents: 27235
diff changeset
81 optfirst = true;
d29a12e8b6d9 unique.m: Set default to "first" when multiple options present.
Rik <rik@octave.org>
parents: 27235
diff changeset
82 endif
d29a12e8b6d9 unique.m: Set default to "first" when multiple options present.
Rik <rik@octave.org>
parents: 27235
diff changeset
83
19003
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18857
diff changeset
84 if (optrows && iscellstr (x))
10882
055b95863699 unique.m: Improve performance by only calculating necessary outputs.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
85 warning ('unique: "rows" is ignored for cell arrays');
7920
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7650
diff changeset
86 optrows = false;
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7650
diff changeset
87 endif
8412
970b4dbff9e4 optimize unique called with a single argument
Daniel J Sebald <daniel.sebald@ieee.org>
parents: 7920
diff changeset
88 else
19003
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18857
diff changeset
89 optrows = false;
27226
6eb32f0aea87 Implement "legacy" flag for intersect function (bug #56592).
Rik <rik@octave.org>
parents: 26376
diff changeset
90 optfirst = true;
6eb32f0aea87 Implement "legacy" flag for intersect function (bug #56592).
Rik <rik@octave.org>
parents: 26376
diff changeset
91 optlegacy = false;
7920
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7650
diff changeset
92 endif
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7650
diff changeset
93
18857
7bbe3658c5ef maint: Use "FIXME:" coding convention in m-files.
Rik <rik@octave.org>
parents: 17744
diff changeset
94 ## FIXME: The operations
10373
1f11fabfa349 improve performance of unique for sparse arrays
John W. Eaton <jwe@octave.org>
parents: 10372
diff changeset
95 ##
1f11fabfa349 improve performance of unique for sparse arrays
John W. Eaton <jwe@octave.org>
parents: 10372
diff changeset
96 ## match = (y(1:n-1) == y(2:n));
1f11fabfa349 improve performance of unique for sparse arrays
John W. Eaton <jwe@octave.org>
parents: 10372
diff changeset
97 ## y(idx) = [];
1f11fabfa349 improve performance of unique for sparse arrays
John W. Eaton <jwe@octave.org>
parents: 10372
diff changeset
98 ##
1f11fabfa349 improve performance of unique for sparse arrays
John W. Eaton <jwe@octave.org>
parents: 10372
diff changeset
99 ## are very slow on sparse matrices. Until they are fixed to be as
1f11fabfa349 improve performance of unique for sparse arrays
John W. Eaton <jwe@octave.org>
parents: 10372
diff changeset
100 ## fast as for full matrices, operate on the nonzero elements of the
1f11fabfa349 improve performance of unique for sparse arrays
John W. Eaton <jwe@octave.org>
parents: 10372
diff changeset
101 ## sparse array as long as we are not operating on rows.
1f11fabfa349 improve performance of unique for sparse arrays
John W. Eaton <jwe@octave.org>
parents: 10372
diff changeset
102
11097
ffb2f1ef2097 make issparse, sparse, and spalloc built-in functions
John W. Eaton <jwe@octave.org>
parents: 10885
diff changeset
103 if (issparse (x) && ! optrows && nargout <= 1)
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11563
diff changeset
104 if (nnz (x) < numel (x))
19003
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18857
diff changeset
105 y = unique ([0; nonzeros(x)], varargin{:});
11097
ffb2f1ef2097 make issparse, sparse, and spalloc built-in functions
John W. Eaton <jwe@octave.org>
parents: 10885
diff changeset
106 else
ffb2f1ef2097 make issparse, sparse, and spalloc built-in functions
John W. Eaton <jwe@octave.org>
parents: 10885
diff changeset
107 ## Corner case where sparse matrix is actually full
ffb2f1ef2097 make issparse, sparse, and spalloc built-in functions
John W. Eaton <jwe@octave.org>
parents: 10885
diff changeset
108 y = unique (full (x), varargin{:});
10373
1f11fabfa349 improve performance of unique for sparse arrays
John W. Eaton <jwe@octave.org>
parents: 10372
diff changeset
109 endif
11097
ffb2f1ef2097 make issparse, sparse, and spalloc built-in functions
John W. Eaton <jwe@octave.org>
parents: 10885
diff changeset
110 return;
10373
1f11fabfa349 improve performance of unique for sparse arrays
John W. Eaton <jwe@octave.org>
parents: 10372
diff changeset
111 endif
1f11fabfa349 improve performance of unique for sparse arrays
John W. Eaton <jwe@octave.org>
parents: 10372
diff changeset
112
7920
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7650
diff changeset
113 if (optrows)
10882
055b95863699 unique.m: Improve performance by only calculating necessary outputs.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
114 n = rows (x);
27228
c80681b4948d unique.m: Fix special case of "legacy" and "rows" flag both present.
Rik <rik@octave.org>
parents: 27226
diff changeset
115 isrowvec = false;
7920
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7650
diff changeset
116 else
6609
30891d1d0c86 [project @ 2007-05-09 02:12:04 by jwe]
jwe
parents: 6046
diff changeset
117 n = numel (x);
27226
6eb32f0aea87 Implement "legacy" flag for intersect function (bug #56592).
Rik <rik@octave.org>
parents: 26376
diff changeset
118 isrowvec = isrow (x);
5165
b822b4895af2 [project @ 2005-02-27 20:51:35 by dbateman]
dbateman
parents:
diff changeset
119 endif
b822b4895af2 [project @ 2005-02-27 20:51:35 by dbateman]
dbateman
parents:
diff changeset
120
5168
912058eb8360 [project @ 2005-02-28 03:23:36 by jwe]
jwe
parents: 5165
diff changeset
121 y = x;
10882
055b95863699 unique.m: Improve performance by only calculating necessary outputs.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
122 ## Special cases 0 and 1
055b95863699 unique.m: Improve performance by only calculating necessary outputs.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
123 if (n == 0)
10372
634e182d34e4 unique.m: return 0x1 for empty matrices with some nonzero dims; preserve class of argument; new tests
John W. Eaton <jwe@octave.org>
parents: 10088
diff changeset
124 if (! optrows && isempty (x) && any (size (x)))
19003
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18857
diff changeset
125 if (iscellstr (y))
10372
634e182d34e4 unique.m: return 0x1 for empty matrices with some nonzero dims; preserve class of argument; new tests
John W. Eaton <jwe@octave.org>
parents: 10088
diff changeset
126 y = cell (0, 1);
634e182d34e4 unique.m: return 0x1 for empty matrices with some nonzero dims; preserve class of argument; new tests
John W. Eaton <jwe@octave.org>
parents: 10088
diff changeset
127 else
634e182d34e4 unique.m: return 0x1 for empty matrices with some nonzero dims; preserve class of argument; new tests
John W. Eaton <jwe@octave.org>
parents: 10088
diff changeset
128 y = zeros (0, 1, class (y));
634e182d34e4 unique.m: return 0x1 for empty matrices with some nonzero dims; preserve class of argument; new tests
John W. Eaton <jwe@octave.org>
parents: 10088
diff changeset
129 endif
634e182d34e4 unique.m: return 0x1 for empty matrices with some nonzero dims; preserve class of argument; new tests
John W. Eaton <jwe@octave.org>
parents: 10088
diff changeset
130 endif
5165
b822b4895af2 [project @ 2005-02-27 20:51:35 by dbateman]
dbateman
parents:
diff changeset
131 i = j = [];
5168
912058eb8360 [project @ 2005-02-28 03:23:36 by jwe]
jwe
parents: 5165
diff changeset
132 return;
10882
055b95863699 unique.m: Improve performance by only calculating necessary outputs.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
133 elseif (n == 1)
5165
b822b4895af2 [project @ 2005-02-27 20:51:35 by dbateman]
dbateman
parents:
diff changeset
134 i = j = 1;
5168
912058eb8360 [project @ 2005-02-28 03:23:36 by jwe]
jwe
parents: 5165
diff changeset
135 return;
5165
b822b4895af2 [project @ 2005-02-27 20:51:35 by dbateman]
dbateman
parents:
diff changeset
136 endif
b822b4895af2 [project @ 2005-02-27 20:51:35 by dbateman]
dbateman
parents:
diff changeset
137
7920
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7650
diff changeset
138 if (optrows)
10882
055b95863699 unique.m: Improve performance by only calculating necessary outputs.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
139 if (nargout > 1)
055b95863699 unique.m: Improve performance by only calculating necessary outputs.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
140 [y, i] = sortrows (y);
27226
6eb32f0aea87 Implement "legacy" flag for intersect function (bug #56592).
Rik <rik@octave.org>
parents: 26376
diff changeset
141 i = i(:);
10882
055b95863699 unique.m: Improve performance by only calculating necessary outputs.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
142 else
055b95863699 unique.m: Improve performance by only calculating necessary outputs.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
143 y = sortrows (y);
055b95863699 unique.m: Improve performance by only calculating necessary outputs.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
144 endif
7920
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7650
diff changeset
145 match = all (y(1:n-1,:) == y(2:n,:), 2);
19003
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18857
diff changeset
146 y(match,:) = [];
5165
b822b4895af2 [project @ 2005-02-27 20:51:35 by dbateman]
dbateman
parents:
diff changeset
147 else
10882
055b95863699 unique.m: Improve performance by only calculating necessary outputs.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
148 if (! isvector (y))
5168
912058eb8360 [project @ 2005-02-28 03:23:36 by jwe]
jwe
parents: 5165
diff changeset
149 y = y(:);
912058eb8360 [project @ 2005-02-28 03:23:36 by jwe]
jwe
parents: 5165
diff changeset
150 endif
10882
055b95863699 unique.m: Improve performance by only calculating necessary outputs.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
151 if (nargout > 1)
055b95863699 unique.m: Improve performance by only calculating necessary outputs.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
152 [y, i] = sort (y);
27226
6eb32f0aea87 Implement "legacy" flag for intersect function (bug #56592).
Rik <rik@octave.org>
parents: 26376
diff changeset
153 i = i(:);
10882
055b95863699 unique.m: Improve performance by only calculating necessary outputs.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
154 else
055b95863699 unique.m: Improve performance by only calculating necessary outputs.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
155 y = sort (y);
055b95863699 unique.m: Improve performance by only calculating necessary outputs.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
156 endif
19003
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18857
diff changeset
157 if (iscellstr (y))
7920
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7650
diff changeset
158 match = strcmp (y(1:n-1), y(2:n));
5205
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents: 5168
diff changeset
159 else
7920
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7650
diff changeset
160 match = (y(1:n-1) == y(2:n));
5205
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents: 5168
diff changeset
161 endif
19003
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18857
diff changeset
162 y(match) = [];
5165
b822b4895af2 [project @ 2005-02-27 20:51:35 by dbateman]
dbateman
parents:
diff changeset
163 endif
b822b4895af2 [project @ 2005-02-27 20:51:35 by dbateman]
dbateman
parents:
diff changeset
164
10882
055b95863699 unique.m: Improve performance by only calculating necessary outputs.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
165 if (isargout (3))
5165
b822b4895af2 [project @ 2005-02-27 20:51:35 by dbateman]
dbateman
parents:
diff changeset
166 j = i;
27226
6eb32f0aea87 Implement "legacy" flag for intersect function (bug #56592).
Rik <rik@octave.org>
parents: 26376
diff changeset
167 j(i) = cumsum ([1; ! match(:)]);
6eb32f0aea87 Implement "legacy" flag for intersect function (bug #56592).
Rik <rik@octave.org>
parents: 26376
diff changeset
168 if (optlegacy && isrowvec)
6eb32f0aea87 Implement "legacy" flag for intersect function (bug #56592).
Rik <rik@octave.org>
parents: 26376
diff changeset
169 j = j.';
8610
85c9906abfd1 use endif and endfor instead of end
John W. Eaton <jwe@octave.org>
parents: 8502
diff changeset
170 endif
5165
b822b4895af2 [project @ 2005-02-27 20:51:35 by dbateman]
dbateman
parents:
diff changeset
171 endif
8412
970b4dbff9e4 optimize unique called with a single argument
Daniel J Sebald <daniel.sebald@ieee.org>
parents: 7920
diff changeset
172
10882
055b95863699 unique.m: Improve performance by only calculating necessary outputs.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
173 if (isargout (2))
19003
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18857
diff changeset
174 idx = find (match);
27236
d29a12e8b6d9 unique.m: Set default to "first" when multiple options present.
Rik <rik@octave.org>
parents: 27235
diff changeset
175 if (! optlegacy && optfirst)
19003
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18857
diff changeset
176 idx += 1; # in-place is faster than other forms of increment
10882
055b95863699 unique.m: Improve performance by only calculating necessary outputs.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
177 endif
19003
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18857
diff changeset
178 i(idx) = [];
27226
6eb32f0aea87 Implement "legacy" flag for intersect function (bug #56592).
Rik <rik@octave.org>
parents: 26376
diff changeset
179 if (optlegacy && isrowvec)
6eb32f0aea87 Implement "legacy" flag for intersect function (bug #56592).
Rik <rik@octave.org>
parents: 26376
diff changeset
180 i = i.';
6eb32f0aea87 Implement "legacy" flag for intersect function (bug #56592).
Rik <rik@octave.org>
parents: 26376
diff changeset
181 endif
7920
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7650
diff changeset
182 endif
5165
b822b4895af2 [project @ 2005-02-27 20:51:35 by dbateman]
dbateman
parents:
diff changeset
183
b822b4895af2 [project @ 2005-02-27 20:51:35 by dbateman]
dbateman
parents:
diff changeset
184 endfunction
b822b4895af2 [project @ 2005-02-27 20:51:35 by dbateman]
dbateman
parents:
diff changeset
185
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
186
19003
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18857
diff changeset
187 %!assert (unique ([1 1 2; 1 2 1; 1 1 2]), [1;2])
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18857
diff changeset
188 %!assert (unique ([1 1 2; 1 0 1; 1 1 2],"rows"), [1 0 1; 1 1 2])
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18857
diff changeset
189 %!assert (unique ([]), [])
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18857
diff changeset
190 %!assert (unique ([1]), [1])
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18857
diff changeset
191 %!assert (unique ([1 2]), [1 2])
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18857
diff changeset
192 %!assert (unique ([1;2]), [1;2])
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18857
diff changeset
193 %!assert (unique ([1,NaN,Inf,NaN,Inf]), [1,Inf,NaN,NaN])
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18857
diff changeset
194 %!assert (unique ({"Foo","Bar","Foo"}), {"Bar","Foo"})
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18857
diff changeset
195 %!assert (unique ({"Foo","Bar","FooBar"}'), {"Bar","Foo","FooBar"}')
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
196 %!assert (unique (zeros (1,0)), zeros (0,1))
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
197 %!assert (unique (zeros (1,0), "rows"), zeros (1,0))
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
198 %!assert (unique (cell (1,0)), cell (0,1))
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
199 %!assert (unique ({}), {})
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
200 %!assert (unique ([1,2,2,3,2,4], "rows"), [1,2,2,3,2,4])
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
201 %!assert (unique ([1,2,2,3,2,4]), [1,2,3,4])
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
202 %!assert (unique ([1,2,2,3,2,4]', "rows"), [1,2,3,4]')
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
203 %!assert (unique (sparse ([2,0;2,0])), [0,2]')
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
204 %!assert (unique (sparse ([1,2;2,3])), [1,2,3]')
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
205 %!assert (unique ([1,2,2,3,2,4]', "rows"), [1,2,3,4]')
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
206 %!assert (unique (single ([1,2,2,3,2,4]), "rows"), single ([1,2,2,3,2,4]))
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
207 %!assert (unique (single ([1,2,2,3,2,4])), single ([1,2,3,4]))
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
208 %!assert (unique (single ([1,2,2,3,2,4]'), "rows"), single ([1,2,3,4]'))
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
209 %!assert (unique (uint8 ([1,2,2,3,2,4]), "rows"), uint8 ([1,2,2,3,2,4]))
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
210 %!assert (unique (uint8 ([1,2,2,3,2,4])), uint8 ([1,2,3,4]))
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
211 %!assert (unique (uint8 ([1,2,2,3,2,4]'), "rows"), uint8 ([1,2,3,4]'))
19003
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18857
diff changeset
212
7920
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7650
diff changeset
213 %!test
27236
d29a12e8b6d9 unique.m: Set default to "first" when multiple options present.
Rik <rik@octave.org>
parents: 27235
diff changeset
214 %! [y,i,j] = unique ([1,1,2,3,3,3,4]);
d29a12e8b6d9 unique.m: Set default to "first" when multiple options present.
Rik <rik@octave.org>
parents: 27235
diff changeset
215 %! assert (y, [1,2,3,4]);
27226
6eb32f0aea87 Implement "legacy" flag for intersect function (bug #56592).
Rik <rik@octave.org>
parents: 26376
diff changeset
216 %! assert (i, [1;3;4;7]);
6eb32f0aea87 Implement "legacy" flag for intersect function (bug #56592).
Rik <rik@octave.org>
parents: 26376
diff changeset
217 %! assert (j, [1;1;2;3;3;3;4]);
6eb32f0aea87 Implement "legacy" flag for intersect function (bug #56592).
Rik <rik@octave.org>
parents: 26376
diff changeset
218
7920
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7650
diff changeset
219 %!test
27236
d29a12e8b6d9 unique.m: Set default to "first" when multiple options present.
Rik <rik@octave.org>
parents: 27235
diff changeset
220 %! [y,i,j] = unique ([1,1,2,3,3,3,4]', "last");
d29a12e8b6d9 unique.m: Set default to "first" when multiple options present.
Rik <rik@octave.org>
parents: 27235
diff changeset
221 %! assert (y, [1,2,3,4]');
27226
6eb32f0aea87 Implement "legacy" flag for intersect function (bug #56592).
Rik <rik@octave.org>
parents: 26376
diff changeset
222 %! assert (i, [2;3;6;7]);
6eb32f0aea87 Implement "legacy" flag for intersect function (bug #56592).
Rik <rik@octave.org>
parents: 26376
diff changeset
223 %! assert (j, [1;1;2;3;3;3;4]);
6eb32f0aea87 Implement "legacy" flag for intersect function (bug #56592).
Rik <rik@octave.org>
parents: 26376
diff changeset
224
8502
d437e8dc18fa make unique work for row vectors
Daniel J Sebald <daniel.sebald@ieee.org>
parents: 8413
diff changeset
225 %!test
27236
d29a12e8b6d9 unique.m: Set default to "first" when multiple options present.
Rik <rik@octave.org>
parents: 27235
diff changeset
226 %! [y,i,j] = unique ({"z"; "z"; "z"});
d29a12e8b6d9 unique.m: Set default to "first" when multiple options present.
Rik <rik@octave.org>
parents: 27235
diff changeset
227 %! assert (y, {"z"});
27226
6eb32f0aea87 Implement "legacy" flag for intersect function (bug #56592).
Rik <rik@octave.org>
parents: 26376
diff changeset
228 %! assert (i, [1]);
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
229 %! assert (j, [1;1;1]);
27226
6eb32f0aea87 Implement "legacy" flag for intersect function (bug #56592).
Rik <rik@octave.org>
parents: 26376
diff changeset
230
8502
d437e8dc18fa make unique work for row vectors
Daniel J Sebald <daniel.sebald@ieee.org>
parents: 8413
diff changeset
231 %!test
27226
6eb32f0aea87 Implement "legacy" flag for intersect function (bug #56592).
Rik <rik@octave.org>
parents: 26376
diff changeset
232 %! A = [1,2,3; 1,2,3];
27236
d29a12e8b6d9 unique.m: Set default to "first" when multiple options present.
Rik <rik@octave.org>
parents: 27235
diff changeset
233 %! [y,i,j] = unique (A, "rows");
d29a12e8b6d9 unique.m: Set default to "first" when multiple options present.
Rik <rik@octave.org>
parents: 27235
diff changeset
234 %! assert (y, [1,2,3]);
d29a12e8b6d9 unique.m: Set default to "first" when multiple options present.
Rik <rik@octave.org>
parents: 27235
diff changeset
235 %! assert (A(i,:), y);
d29a12e8b6d9 unique.m: Set default to "first" when multiple options present.
Rik <rik@octave.org>
parents: 27235
diff changeset
236 %! assert (y(j,:), A);
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
237
27226
6eb32f0aea87 Implement "legacy" flag for intersect function (bug #56592).
Rik <rik@octave.org>
parents: 26376
diff changeset
238 %!test
27236
d29a12e8b6d9 unique.m: Set default to "first" when multiple options present.
Rik <rik@octave.org>
parents: 27235
diff changeset
239 %! [y,i,j] = unique ([1,1,2,3,3,3,4], "legacy");
d29a12e8b6d9 unique.m: Set default to "first" when multiple options present.
Rik <rik@octave.org>
parents: 27235
diff changeset
240 %! assert (y, [1,2,3,4]);
27226
6eb32f0aea87 Implement "legacy" flag for intersect function (bug #56592).
Rik <rik@octave.org>
parents: 26376
diff changeset
241 %! assert (i, [2,3,6,7]);
6eb32f0aea87 Implement "legacy" flag for intersect function (bug #56592).
Rik <rik@octave.org>
parents: 26376
diff changeset
242 %! assert (j, [1,1,2,3,3,3,4]);
6eb32f0aea87 Implement "legacy" flag for intersect function (bug #56592).
Rik <rik@octave.org>
parents: 26376
diff changeset
243
27228
c80681b4948d unique.m: Fix special case of "legacy" and "rows" flag both present.
Rik <rik@octave.org>
parents: 27226
diff changeset
244 %!test
27236
d29a12e8b6d9 unique.m: Set default to "first" when multiple options present.
Rik <rik@octave.org>
parents: 27235
diff changeset
245 %! A = [7 9 7; 0 0 0; 7 9 7; 5 5 5; 1 4 5];
d29a12e8b6d9 unique.m: Set default to "first" when multiple options present.
Rik <rik@octave.org>
parents: 27235
diff changeset
246 %! [y,i,j] = unique (A, "rows", "legacy");
d29a12e8b6d9 unique.m: Set default to "first" when multiple options present.
Rik <rik@octave.org>
parents: 27235
diff changeset
247 %! assert (y, [0 0 0; 1 4 5; 5 5 5; 7 9 7]);
d29a12e8b6d9 unique.m: Set default to "first" when multiple options present.
Rik <rik@octave.org>
parents: 27235
diff changeset
248 %! assert (i, [2; 5; 4; 3]);
d29a12e8b6d9 unique.m: Set default to "first" when multiple options present.
Rik <rik@octave.org>
parents: 27235
diff changeset
249 %! assert (j, [4; 1; 4; 3; 2]);
27228
c80681b4948d unique.m: Fix special case of "legacy" and "rows" flag both present.
Rik <rik@octave.org>
parents: 27226
diff changeset
250
19833
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19700
diff changeset
251 ## Test input validation
19003
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18857
diff changeset
252 %!error unique ()
19700
00e31f316a3a Fix Matlab incompatibility of "ismatrix" (bug #42422).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 19697
diff changeset
253 %!error <X must be an array or cell array of strings> unique ({1})
19003
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18857
diff changeset
254 %!error <options must be strings> unique (1, 2)
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18857
diff changeset
255 %!error <cannot specify both "first" and "last"> unique (1, "first", "last")
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18857
diff changeset
256 %!error <invalid option> unique (1, "middle")
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18857
diff changeset
257 %!error <invalid option> unique ({"a", "b", "c"}, "UnknownOption")
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18857
diff changeset
258 %!error <invalid option> unique ({"a", "b", "c"}, "UnknownOption1", "UnknownOption2")
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18857
diff changeset
259 %!error <invalid option> unique ({"a", "b", "c"}, "rows", "UnknownOption2")
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18857
diff changeset
260 %!error <invalid option> unique ({"a", "b", "c"}, "UnknownOption1", "last")
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18857
diff changeset
261 %!warning <"rows" is ignored for cell arrays> unique ({"1"}, "rows");