annotate scripts/set/unique.m @ 19734:00e31f316a3a

Fix Matlab incompatibility of "ismatrix" (bug #42422). * data.cc (isvector): new tests * data.cc (isrow): documentation improved, new tests * data.cc (iscolumn): documentation improved, new tests * data.cc (ismatrix): is matrix now only checks the dimension due to Matlab compatibility, documentation improved, new tests * accumarray.m: use more appropriate function, than ismatrix * gradient.m: use more appropriate function, than ismatrix * num2str.m: use more appropriate functions, than ismatrix * ntsc2rgb.m: use more appropriate function, than ismatrix * condest.m: use more appropriate function, than ismatrix * expm.m: use more appropriate function, than ismatrix * onenormest.m: use more appropriate function, than ismatrix * isocolors.m: use more appropriate function, than ismatrix * isonormals.m: use more appropriate function, than ismatrix * isosurface.m: use more appropriate function, than ismatrix * __errcomm__.m: use more appropriate function, than ismatrix * __interp_cube__.m: use more appropriate function, than ismatrix * __marching_cube__.m: use more appropriate function, than ismatrix * __stem__.m: use more appropriate function, than ismatrix * stairs.m: use more appropriate function, than ismatrix * validsetargs.m: use more appropriate functions, than ismatrix * unique.m: use more appropriate functions, than ismatrix * bicg.m: additional tests for numerical value * bicgstab.m: additional tests for numerical value * cgs.m: additional tests for numerical value * gmres.m: additional tests for numerical value * qmr.m: additional tests for numerical value
author Kai T. Ohlhus <k.ohlhus@gmail.com>
date Thu, 12 Feb 2015 18:34:56 +0100
parents 4197fc428c7d
children 9fc020886ae9
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: 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 ##
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6609
diff changeset
6 ## Octave is free software; you can redistribute it and/or modify it
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
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6609
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: 6609
diff changeset
9 ## 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
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6609
diff changeset
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6609
diff changeset
14 ## 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
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6609
diff changeset
18 ## <http://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 -*-
10793
be55736a0783 Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents: 10373
diff changeset
21 ## @deftypefn {Function File} {} unique (@var{x})
8887
6e4a811e58f8 deprecate create_set
John W. Eaton <jwe@octave.org>
parents: 8664
diff changeset
22 ## @deftypefnx {Function File} {} unique (@var{x}, "rows")
6e4a811e58f8 deprecate create_set
John W. Eaton <jwe@octave.org>
parents: 8664
diff changeset
23 ## @deftypefnx {Function File} {[@var{y}, @var{i}, @var{j}] =} unique (@dots{})
19037
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18891
diff changeset
24 ## @deftypefnx {Function File} {[@var{y}, @var{i}, @var{j}] =} unique (@dots{}, "first")
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18891
diff changeset
25 ## @deftypefnx {Function File} {[@var{y}, @var{i}, @var{j}] =} unique (@dots{}, "last")
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18891
diff changeset
26 ## 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
27 ##
19037
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18891
diff changeset
28 ## If the input @var{x} is a column vector then return a column vector;
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18891
diff changeset
29 ## Otherwise, return a row vector. @var{x} may also be a cell array of strings.
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18891
diff changeset
30 ##
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18891
diff changeset
31 ## If the optional argument @qcode{"rows"} is given then return the unique
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18891
diff changeset
32 ## 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: 18891
diff changeset
33 ## to use this option.
5165
b822b4895af2 [project @ 2005-02-27 20:51:35 by dbateman]
dbateman
parents:
diff changeset
34 ##
8887
6e4a811e58f8 deprecate create_set
John W. Eaton <jwe@octave.org>
parents: 8664
diff changeset
35 ## If requested, return index vectors @var{i} and @var{j} such that
19037
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18891
diff changeset
36 ## @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
37 ##
17281
bc924baa2c4e doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents: 16221
diff changeset
38 ## Additionally, if @var{i} is a requested output then one of @qcode{"first"} or
bc924baa2c4e doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents: 16221
diff changeset
39 ## @qcode{"last"} may be given as an input. If @qcode{"last"} is specified,
bc924baa2c4e doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents: 16221
diff changeset
40 ## return the highest possible indices in @var{i}, otherwise, if @qcode{"first"}
bc924baa2c4e doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents: 16221
diff changeset
41 ## is specified, return the lowest. The default is @qcode{"last"}.
5642
2618a0750ae6 [project @ 2006-03-06 21:26:48 by jwe]
jwe
parents: 5532
diff changeset
42 ## @seealso{union, intersect, setdiff, setxor, ismember}
5165
b822b4895af2 [project @ 2005-02-27 20:51:35 by dbateman]
dbateman
parents:
diff changeset
43 ## @end deftypefn
b822b4895af2 [project @ 2005-02-27 20:51:35 by dbateman]
dbateman
parents:
diff changeset
44
7920
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7650
diff changeset
45 function [y, i, j] = unique (x, varargin)
5165
b822b4895af2 [project @ 2005-02-27 20:51:35 by dbateman]
dbateman
parents:
diff changeset
46
7920
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7650
diff changeset
47 if (nargin < 1)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5737
diff changeset
48 print_usage ();
19734
00e31f316a3a Fix Matlab incompatibility of "ismatrix" (bug #42422).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 19731
diff changeset
49 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: 19731
diff changeset
50 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
51 endif
b822b4895af2 [project @ 2005-02-27 20:51:35 by dbateman]
dbateman
parents:
diff changeset
52
8412
970b4dbff9e4 optimize unique called with a single argument
Daniel J Sebald <daniel.sebald@ieee.org>
parents: 7920
diff changeset
53 if (nargin > 1)
970b4dbff9e4 optimize unique called with a single argument
Daniel J Sebald <daniel.sebald@ieee.org>
parents: 7920
diff changeset
54 ## parse options
19037
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18891
diff changeset
55 if (! iscellstr (varargin))
8412
970b4dbff9e4 optimize unique called with a single argument
Daniel J Sebald <daniel.sebald@ieee.org>
parents: 7920
diff changeset
56 error ("unique: options must be strings");
7920
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7650
diff changeset
57 endif
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7650
diff changeset
58
19037
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18891
diff changeset
59 optrows = any (strcmp ("rows", varargin));
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18891
diff changeset
60 optfirst = any (strcmp ("first", varargin));
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18891
diff changeset
61 optlast = any (strcmp ("last", varargin));
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18891
diff changeset
62 if (optfirst && optlast)
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18891
diff changeset
63 error ('unique: cannot specify both "first" and "last"');
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18891
diff changeset
64 elseif (optfirst + optlast + optrows != nargin-1)
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18891
diff changeset
65 error ("unique: invalid option");
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18891
diff changeset
66 endif
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18891
diff changeset
67
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18891
diff changeset
68 if (optrows && iscellstr (x))
10882
055b95863699 unique.m: Improve performance by only calculating necessary outputs.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
69 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
70 optrows = false;
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7650
diff changeset
71 endif
8412
970b4dbff9e4 optimize unique called with a single argument
Daniel J Sebald <daniel.sebald@ieee.org>
parents: 7920
diff changeset
72 else
19037
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18891
diff changeset
73 optrows = false;
10882
055b95863699 unique.m: Improve performance by only calculating necessary outputs.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
74 optfirst = false;
7920
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7650
diff changeset
75 endif
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7650
diff changeset
76
18891
7bbe3658c5ef maint: Use "FIXME:" coding convention in m-files.
Rik <rik@octave.org>
parents: 17744
diff changeset
77 ## FIXME: The operations
10373
1f11fabfa349 improve performance of unique for sparse arrays
John W. Eaton <jwe@octave.org>
parents: 10372
diff changeset
78 ##
1f11fabfa349 improve performance of unique for sparse arrays
John W. Eaton <jwe@octave.org>
parents: 10372
diff changeset
79 ## 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
80 ## y(idx) = [];
1f11fabfa349 improve performance of unique for sparse arrays
John W. Eaton <jwe@octave.org>
parents: 10372
diff changeset
81 ##
1f11fabfa349 improve performance of unique for sparse arrays
John W. Eaton <jwe@octave.org>
parents: 10372
diff changeset
82 ## 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
83 ## 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
84 ## 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
85
11097
ffb2f1ef2097 make issparse, sparse, and spalloc built-in functions
John W. Eaton <jwe@octave.org>
parents: 10885
diff changeset
86 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
87 if (nnz (x) < numel (x))
19037
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18891
diff changeset
88 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
89 else
ffb2f1ef2097 make issparse, sparse, and spalloc built-in functions
John W. Eaton <jwe@octave.org>
parents: 10885
diff changeset
90 ## 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
91 y = unique (full (x), varargin{:});
10373
1f11fabfa349 improve performance of unique for sparse arrays
John W. Eaton <jwe@octave.org>
parents: 10372
diff changeset
92 endif
11097
ffb2f1ef2097 make issparse, sparse, and spalloc built-in functions
John W. Eaton <jwe@octave.org>
parents: 10885
diff changeset
93 return;
10373
1f11fabfa349 improve performance of unique for sparse arrays
John W. Eaton <jwe@octave.org>
parents: 10372
diff changeset
94 endif
1f11fabfa349 improve performance of unique for sparse arrays
John W. Eaton <jwe@octave.org>
parents: 10372
diff changeset
95
7920
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7650
diff changeset
96 if (optrows)
10882
055b95863699 unique.m: Improve performance by only calculating necessary outputs.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
97 n = rows (x);
8502
d437e8dc18fa make unique work for row vectors
Daniel J Sebald <daniel.sebald@ieee.org>
parents: 8413
diff changeset
98 dim = 1;
7920
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7650
diff changeset
99 else
6609
30891d1d0c86 [project @ 2007-05-09 02:12:04 by jwe]
jwe
parents: 6046
diff changeset
100 n = numel (x);
10882
055b95863699 unique.m: Improve performance by only calculating necessary outputs.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
101 dim = (rows (x) == 1) + 1;
5165
b822b4895af2 [project @ 2005-02-27 20:51:35 by dbateman]
dbateman
parents:
diff changeset
102 endif
b822b4895af2 [project @ 2005-02-27 20:51:35 by dbateman]
dbateman
parents:
diff changeset
103
5168
912058eb8360 [project @ 2005-02-28 03:23:36 by jwe]
jwe
parents: 5165
diff changeset
104 y = x;
10882
055b95863699 unique.m: Improve performance by only calculating necessary outputs.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
105 ## Special cases 0 and 1
055b95863699 unique.m: Improve performance by only calculating necessary outputs.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
106 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
107 if (! optrows && isempty (x) && any (size (x)))
19037
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18891
diff changeset
108 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
109 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
110 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
111 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
112 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
113 endif
5165
b822b4895af2 [project @ 2005-02-27 20:51:35 by dbateman]
dbateman
parents:
diff changeset
114 i = j = [];
5168
912058eb8360 [project @ 2005-02-28 03:23:36 by jwe]
jwe
parents: 5165
diff changeset
115 return;
10882
055b95863699 unique.m: Improve performance by only calculating necessary outputs.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
116 elseif (n == 1)
5165
b822b4895af2 [project @ 2005-02-27 20:51:35 by dbateman]
dbateman
parents:
diff changeset
117 i = j = 1;
5168
912058eb8360 [project @ 2005-02-28 03:23:36 by jwe]
jwe
parents: 5165
diff changeset
118 return;
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
7920
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7650
diff changeset
121 if (optrows)
10882
055b95863699 unique.m: Improve performance by only calculating necessary outputs.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
122 if (nargout > 1)
055b95863699 unique.m: Improve performance by only calculating necessary outputs.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
123 [y, i] = sortrows (y);
055b95863699 unique.m: Improve performance by only calculating necessary outputs.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
124 else
055b95863699 unique.m: Improve performance by only calculating necessary outputs.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
125 y = sortrows (y);
055b95863699 unique.m: Improve performance by only calculating necessary outputs.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
126 endif
7920
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7650
diff changeset
127 match = all (y(1:n-1,:) == y(2:n,:), 2);
19037
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18891
diff changeset
128 y(match,:) = [];
5165
b822b4895af2 [project @ 2005-02-27 20:51:35 by dbateman]
dbateman
parents:
diff changeset
129 else
10882
055b95863699 unique.m: Improve performance by only calculating necessary outputs.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
130 if (! isvector (y))
5168
912058eb8360 [project @ 2005-02-28 03:23:36 by jwe]
jwe
parents: 5165
diff changeset
131 y = y(:);
912058eb8360 [project @ 2005-02-28 03:23:36 by jwe]
jwe
parents: 5165
diff changeset
132 endif
10882
055b95863699 unique.m: Improve performance by only calculating necessary outputs.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
133 if (nargout > 1)
055b95863699 unique.m: Improve performance by only calculating necessary outputs.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
134 [y, i] = sort (y);
055b95863699 unique.m: Improve performance by only calculating necessary outputs.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
135 else
055b95863699 unique.m: Improve performance by only calculating necessary outputs.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
136 y = sort (y);
055b95863699 unique.m: Improve performance by only calculating necessary outputs.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
137 endif
19037
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18891
diff changeset
138 if (iscellstr (y))
7920
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7650
diff changeset
139 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
140 else
7920
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7650
diff changeset
141 match = (y(1:n-1) == y(2:n));
5205
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents: 5168
diff changeset
142 endif
19037
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18891
diff changeset
143 y(match) = [];
5165
b822b4895af2 [project @ 2005-02-27 20:51:35 by dbateman]
dbateman
parents:
diff changeset
144 endif
b822b4895af2 [project @ 2005-02-27 20:51:35 by dbateman]
dbateman
parents:
diff changeset
145
10882
055b95863699 unique.m: Improve performance by only calculating necessary outputs.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
146 if (isargout (3))
5165
b822b4895af2 [project @ 2005-02-27 20:51:35 by dbateman]
dbateman
parents:
diff changeset
147 j = i;
8502
d437e8dc18fa make unique work for row vectors
Daniel J Sebald <daniel.sebald@ieee.org>
parents: 8413
diff changeset
148 if (dim == 1)
d437e8dc18fa make unique work for row vectors
Daniel J Sebald <daniel.sebald@ieee.org>
parents: 8413
diff changeset
149 j(i) = cumsum ([1; !match]);
d437e8dc18fa make unique work for row vectors
Daniel J Sebald <daniel.sebald@ieee.org>
parents: 8413
diff changeset
150 else
d437e8dc18fa make unique work for row vectors
Daniel J Sebald <daniel.sebald@ieee.org>
parents: 8413
diff changeset
151 j(i) = cumsum ([1, !match]);
8610
85c9906abfd1 use endif and endfor instead of end
John W. Eaton <jwe@octave.org>
parents: 8502
diff changeset
152 endif
5165
b822b4895af2 [project @ 2005-02-27 20:51:35 by dbateman]
dbateman
parents:
diff changeset
153 endif
8412
970b4dbff9e4 optimize unique called with a single argument
Daniel J Sebald <daniel.sebald@ieee.org>
parents: 7920
diff changeset
154
10882
055b95863699 unique.m: Improve performance by only calculating necessary outputs.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
155 if (isargout (2))
19037
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18891
diff changeset
156 idx = find (match);
10882
055b95863699 unique.m: Improve performance by only calculating necessary outputs.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
157 if (optfirst)
19037
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18891
diff changeset
158 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
159 endif
19037
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18891
diff changeset
160 i(idx) = [];
7920
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7650
diff changeset
161 endif
5165
b822b4895af2 [project @ 2005-02-27 20:51:35 by dbateman]
dbateman
parents:
diff changeset
162
b822b4895af2 [project @ 2005-02-27 20:51:35 by dbateman]
dbateman
parents:
diff changeset
163 endfunction
b822b4895af2 [project @ 2005-02-27 20:51:35 by dbateman]
dbateman
parents:
diff changeset
164
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
165
19037
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18891
diff changeset
166 %!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: 18891
diff changeset
167 %!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: 18891
diff changeset
168 %!assert (unique ([]), [])
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18891
diff changeset
169 %!assert (unique ([1]), [1])
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18891
diff changeset
170 %!assert (unique ([1 2]), [1 2])
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18891
diff changeset
171 %!assert (unique ([1;2]), [1;2])
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18891
diff changeset
172 %!assert (unique ([1,NaN,Inf,NaN,Inf]), [1,Inf,NaN,NaN])
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18891
diff changeset
173 %!assert (unique ({"Foo","Bar","Foo"}), {"Bar","Foo"})
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18891
diff changeset
174 %!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
175 %!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
176 %!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
177 %!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
178 %!assert (unique ({}), {})
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
179 %!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
180 %!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
181 %!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
182 %!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
183 %!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
184 %!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
185 %!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
186 %!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
187 %!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
188 %!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
189 %!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
190 %!assert (unique (uint8 ([1,2,2,3,2,4]'), "rows"), uint8 ([1,2,3,4]'))
19037
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18891
diff changeset
191
7920
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7650
diff changeset
192 %!test
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
193 %! [a,i,j] = unique ([1,1,2,3,3,3,4]);
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
194 %! assert (a, [1,2,3,4]);
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
195 %! assert (i, [2,3,6,7]);
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
196 %! assert (j, [1,1,2,3,3,3,4]);
7920
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7650
diff changeset
197 %!
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7650
diff changeset
198 %!test
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
199 %! [a,i,j] = unique ([1,1,2,3,3,3,4]', "first");
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
200 %! assert (a, [1,2,3,4]');
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
201 %! assert (i, [1,3,4,7]');
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
202 %! assert (j, [1,1,2,3,3,3,4]');
8502
d437e8dc18fa make unique work for row vectors
Daniel J Sebald <daniel.sebald@ieee.org>
parents: 8413
diff changeset
203 %!
d437e8dc18fa make unique work for row vectors
Daniel J Sebald <daniel.sebald@ieee.org>
parents: 8413
diff changeset
204 %!test
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
205 %! [a,i,j] = unique ({"z"; "z"; "z"});
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
206 %! assert (a, {"z"});
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
207 %! assert (i, [3]');
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
208 %! assert (j, [1;1;1]);
8502
d437e8dc18fa make unique work for row vectors
Daniel J Sebald <daniel.sebald@ieee.org>
parents: 8413
diff changeset
209 %!
d437e8dc18fa make unique work for row vectors
Daniel J Sebald <daniel.sebald@ieee.org>
parents: 8413
diff changeset
210 %!test
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
211 %! A = [1,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
212 %! [a,i,j] = unique (A, "rows");
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
213 %! assert (a, [1,2,3]);
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
214 %! assert (A(i,:), a);
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
215 %! assert (a(j,:), A);
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
216
19037
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18891
diff changeset
217 %% Test input validation
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18891
diff changeset
218 %!error unique ()
19734
00e31f316a3a Fix Matlab incompatibility of "ismatrix" (bug #42422).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 19731
diff changeset
219 %!error <X must be an array or cell array of strings> unique ({1})
19037
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18891
diff changeset
220 %!error <options must be strings> unique (1, 2)
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18891
diff changeset
221 %!error <cannot specify both "first" and "last"> unique (1, "first", "last")
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18891
diff changeset
222 %!error <invalid option> unique (1, "middle")
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18891
diff changeset
223 %!error <invalid option> unique ({"a", "b", "c"}, "UnknownOption")
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18891
diff changeset
224 %!error <invalid option> unique ({"a", "b", "c"}, "UnknownOption1", "UnknownOption2")
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18891
diff changeset
225 %!error <invalid option> unique ({"a", "b", "c"}, "rows", "UnknownOption2")
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18891
diff changeset
226 %!error <invalid option> unique ({"a", "b", "c"}, "UnknownOption1", "last")
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18891
diff changeset
227 %!warning <"rows" is ignored for cell arrays> unique ({"1"}, "rows");
17338
1c89599167a6 maint: End m-files with 1 blank line.
Rik <rik@octave.org>
parents: 17281
diff changeset
228