annotate scripts/set/unique.m @ 28896:90fea9cc9caa

test: Add expected error message <Invalid call> to BIST tests for nargin. * acosd.m, acot.m, acotd.m, acoth.m, acsc.m, acscd.m, acsch.m, asec.m, asecd.m, asech.m, asind.m, atand.m, cosd.m, cot.m, cotd.m, coth.m, csc.m, cscd.m, csch.m, sec.m, secd.m, sech.m, sind.m, tand.m, cart2pol.m, cart2sph.m, celldisp.m, common_size.m, deal.m, del2.m, fliplr.m, integral2.m, interp1.m, isequal.m, isequaln.m, nextpow2.m, pol2cart.m, quad2d.m, quadl.m, quadv.m, randi.m, rat.m, repelem.m, rescale.m, shiftdim.m, sortrows.m, sph2cart.m, xor.m, convhull.m, delaunay.m, delaunayn.m, griddata.m, griddatan.m, inpolygon.m, voronoi.m, voronoin.m, listdlg.m, msgbox.m, questdlg.m, rmappdata.m, setappdata.m, __gripe_missing_component__.m, get_first_help_sentence.m, type.m, which.m, cmpermute.m, cmunique.m, gray2ind.m, imfinfo.m, imshow.m, imwrite.m, ind2rgb.m, movie.m, rgb2ind.m, importdata.m, bandwidth.m, condeig.m, gls.m, housh.m, linsolve.m, logm.m, lscov.m, normest.m, normest1.m, ols.m, ordeig.m, planerot.m, qzhess.m, rref.m, copyfile.m, delete.m, dos.m, fileparts.m, getfield.m, menu.m, mkdir.m, movefile.m, orderfields.m, publish.m, setfield.m, substruct.m, unix.m, unpack.m, decic.m, ode23.m, ode23s.m, ode45.m, fminsearch.m, lsqnonneg.m, pqpnonneg.m, sqp.m, annotation.m, lighting.m, shading.m, area.m, compass.m, contourc.m, feather.m, fplot.m, hist.m, isocaps.m, isocolors.m, isonormals.m, isosurface.m, ostreamtube.m, pie.m, pie3.m, reducepatch.m, reducevolume.m, rose.m, smooth3.m, stairs.m, stem.m, stem3.m, stream2.m, stream3.m, streamline.m, streamribbon.m, streamtube.m, surfnorm.m, trimesh.m, trisurf.m, colstyle.m, hgload.m, linkprop.m, meshgrid.m, ndgrid.m, padecoef.m, polyfit.m, polyval.m, unmkpp.m, profexport.m, ismember.m, unique.m, movfun.m, movslice.m, periodogram.m, sinc.m, spdiags.m, sprandsym.m, betaincinv.m, ellipke.m, factor.m, gammainc.m, gammaincinv.m, isprime.m, lcm.m, gallery.m, hadamard.m, bounds.m, corrcoef.m, discrete_rnd.m, empirical_rnd.m, histc.m, mode.m, movmad.m, movmax.m, movmean.m, movmedian.m, movmin.m, movprod.m, movstd.m, movsum.m, movvar.m, ranks.m, runlength.m, zscore.m, str2num.m, strchr.m, strsplit.m, strtok.m, untabify.m, assert.m, demo.m, example.m, speed.m, test.m, datenum.m, datevec.m, webread.m, webwrite.m: Add expected error message <Invalid call> to BIST tests for nargin. Remove redundant tests for nargin greater than the number of declared inputs which are now handled by interpreter.
author Rik <rik@octave.org>
date Sun, 11 Oct 2020 21:59:35 -0700
parents bd51beb6205e
children 7854d5752dd2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
1 ########################################################################
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
2 ##
27919
1891570abac8 update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents: 27918
diff changeset
3 ## Copyright (C) 2000-2020 The Octave Project Developers
27918
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 27794
diff changeset
4 ##
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
5 ## See the file COPYRIGHT.md in the top-level directory of this
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
6 ## distribution or <https://octave.org/copyright/>.
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6609
diff changeset
7 ##
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6609
diff changeset
8 ## This file is part of Octave.
5165
b822b4895af2 [project @ 2005-02-27 20:51:35 by dbateman]
dbateman
parents:
diff changeset
9 ##
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23220
diff changeset
10 ## 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
11 ## 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
12 ## 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
13 ## (at your option) any later version.
5165
b822b4895af2 [project @ 2005-02-27 20:51:35 by dbateman]
dbateman
parents:
diff changeset
14 ##
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6609
diff changeset
15 ## 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
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
18 ## GNU General Public License for more details.
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 ## 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
21 ## 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
22 ## <https://www.gnu.org/licenses/>.
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
23 ##
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
24 ########################################################################
5165
b822b4895af2 [project @ 2005-02-27 20:51:35 by dbateman]
dbateman
parents:
diff changeset
25
b822b4895af2 [project @ 2005-02-27 20:51:35 by dbateman]
dbateman
parents:
diff changeset
26 ## -*- texinfo -*-
20852
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 19833
diff changeset
27 ## @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
28 ## @deftypefnx {} {} unique (@var{x}, "rows")
27291
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
29 ## @deftypefnx {} {} unique (@dots{}, "sorted")
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
30 ## @deftypefnx {} {} unique (@dots{}, "stable")
20852
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 19833
diff changeset
31 ## @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
32 ## @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
33 ## @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
34 ## @deftypefnx {} {[@var{y}, @var{i}, @var{j}] =} unique (@dots{}, "legacy")
27291
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
35 ## Return the unique elements of @var{x}.
5165
b822b4895af2 [project @ 2005-02-27 20:51:35 by dbateman]
dbateman
parents:
diff changeset
36 ##
19003
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18857
diff changeset
37 ## 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
38 ## 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
39 ## strings.
19003
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18857
diff changeset
40 ##
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18857
diff changeset
41 ## If the optional argument @qcode{"rows"} is given then return the unique
27291
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
42 ## rows of @var{x}. The input must be a 2-D numeric matrix to use this option.
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
43 ##
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
44 ## The optional argument @qcode{"sorted"}/@qcode{"stable"} controls the order
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
45 ## in which unique values appear in the output. The default is
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
46 ## @qcode{"sorted"} and values in the output are placed in ascending order.
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
47 ## The alternative @qcode{"stable"} preserves the order found in the input
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
48 ## @var{x}.
5165
b822b4895af2 [project @ 2005-02-27 20:51:35 by dbateman]
dbateman
parents:
diff changeset
49 ##
27226
6eb32f0aea87 Implement "legacy" flag for intersect function (bug #56592).
Rik <rik@octave.org>
parents: 26376
diff changeset
50 ## 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
51 ## @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
52 ##
27226
6eb32f0aea87 Implement "legacy" flag for intersect function (bug #56592).
Rik <rik@octave.org>
parents: 26376
diff changeset
53 ## 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
54 ## @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
55 ## 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
56 ## @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
57 ## @qcode{"first"}.
6eb32f0aea87 Implement "legacy" flag for intersect function (bug #56592).
Rik <rik@octave.org>
parents: 26376
diff changeset
58 ##
27291
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
59 ## Example 1 : sort order
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
60 ##
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
61 ## @example
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
62 ## @group
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
63 ## unique ([3, 1, 1, 2])
27794
45ad2127582b doc: grammarcheck scripts before 6.1 release.
Rik <rik@octave.org>
parents: 27296
diff changeset
64 ## @result{} [1, 2, 3]
27291
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
65 ## unique ([3, 1, 1, 2], "stable")
27794
45ad2127582b doc: grammarcheck scripts before 6.1 release.
Rik <rik@octave.org>
parents: 27296
diff changeset
66 ## @result{} [3, 1, 2]
27291
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
67 ## @end group
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
68 ## @end example
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
69 ##
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
70 ## Example 2 : index selection
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
71 ##
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
72 ## @example
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
73 ## @group
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
74 ## [~, @var{i}] = unique ([3, 1, 1, 2], "first")
27794
45ad2127582b doc: grammarcheck scripts before 6.1 release.
Rik <rik@octave.org>
parents: 27296
diff changeset
75 ## @result{} @var{i} = [2; 4; 1]
27291
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
76 ## [~, @var{i}] = unique ([3, 1, 1, 2], "last")
27794
45ad2127582b doc: grammarcheck scripts before 6.1 release.
Rik <rik@octave.org>
parents: 27296
diff changeset
77 ## @result{} @var{i} = [3; 4; 1]
27291
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
78 ## @end group
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
79 ## @end example
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
80 ##
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
81 ## Programming Notes: The input flag @qcode{"legacy"} changes the algorithm
27235
e12571df6466 doc: Update description of "legacy" option for set functions.
Rik <rik@octave.org>
parents: 27228
diff changeset
82 ## 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
83 ## 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
84 ## 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
85 ## than always being column vectors.
27226
6eb32f0aea87 Implement "legacy" flag for intersect function (bug #56592).
Rik <rik@octave.org>
parents: 26376
diff changeset
86 ##
27291
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
87 ## The third output, @var{j}, has not been implemented yet when the sort
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
88 ## order is @qcode{"stable"}.
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
89 ##
5642
2618a0750ae6 [project @ 2006-03-06 21:26:48 by jwe]
jwe
parents: 5532
diff changeset
90 ## @seealso{union, intersect, setdiff, setxor, ismember}
5165
b822b4895af2 [project @ 2005-02-27 20:51:35 by dbateman]
dbateman
parents:
diff changeset
91 ## @end deftypefn
b822b4895af2 [project @ 2005-02-27 20:51:35 by dbateman]
dbateman
parents:
diff changeset
92
7920
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7650
diff changeset
93 function [y, i, j] = unique (x, varargin)
5165
b822b4895af2 [project @ 2005-02-27 20:51:35 by dbateman]
dbateman
parents:
diff changeset
94
7920
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7650
diff changeset
95 if (nargin < 1)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5737
diff changeset
96 print_usage ();
19700
00e31f316a3a Fix Matlab incompatibility of "ismatrix" (bug #42422).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 19697
diff changeset
97 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
98 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
99 endif
b822b4895af2 [project @ 2005-02-27 20:51:35 by dbateman]
dbateman
parents:
diff changeset
100
8412
970b4dbff9e4 optimize unique called with a single argument
Daniel J Sebald <daniel.sebald@ieee.org>
parents: 7920
diff changeset
101 if (nargin > 1)
970b4dbff9e4 optimize unique called with a single argument
Daniel J Sebald <daniel.sebald@ieee.org>
parents: 7920
diff changeset
102 ## parse options
19003
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18857
diff changeset
103 if (! iscellstr (varargin))
8412
970b4dbff9e4 optimize unique called with a single argument
Daniel J Sebald <daniel.sebald@ieee.org>
parents: 7920
diff changeset
104 error ("unique: options must be strings");
7920
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7650
diff changeset
105 endif
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7650
diff changeset
106
27226
6eb32f0aea87 Implement "legacy" flag for intersect function (bug #56592).
Rik <rik@octave.org>
parents: 26376
diff changeset
107 optrows = any (strcmp ("rows", varargin));
6eb32f0aea87 Implement "legacy" flag for intersect function (bug #56592).
Rik <rik@octave.org>
parents: 26376
diff changeset
108 optfirst = any (strcmp ("first", varargin));
6eb32f0aea87 Implement "legacy" flag for intersect function (bug #56592).
Rik <rik@octave.org>
parents: 26376
diff changeset
109 optlast = any (strcmp ("last", varargin));
27291
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
110 optsorted = any (strcmp ("sorted", varargin));
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
111 optstable = any (strcmp ("stable", varargin));
27226
6eb32f0aea87 Implement "legacy" flag for intersect function (bug #56592).
Rik <rik@octave.org>
parents: 26376
diff changeset
112 optlegacy = any (strcmp ("legacy", varargin));
19003
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18857
diff changeset
113 if (optfirst && optlast)
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18857
diff changeset
114 error ('unique: cannot specify both "first" and "last"');
27291
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
115 elseif (optsorted && optstable)
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
116 error ('unique: cannot specify both "sorted" and "stable"');
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
117 elseif ((optfirst || optlast) && (optsorted || optstable))
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
118 error ('unique: cannot specify "first"/"last" with "sorted"/"stable"');
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
119 elseif (optlegacy && (optsorted || optstable))
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
120 error ('unique: cannot specify "sorted" or "stable" with "legacy"');
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
121 elseif (optrows + optfirst + optlast + optsorted + optstable + optlegacy
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
122 != nargin-1)
19003
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18857
diff changeset
123 error ("unique: invalid option");
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18857
diff changeset
124 endif
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18857
diff changeset
125
27291
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
126 ## Set defaults if not set earlier.
27236
d29a12e8b6d9 unique.m: Set default to "first" when multiple options present.
Rik <rik@octave.org>
parents: 27235
diff changeset
127 if (! optfirst && ! optlast)
d29a12e8b6d9 unique.m: Set default to "first" when multiple options present.
Rik <rik@octave.org>
parents: 27235
diff changeset
128 optfirst = true;
d29a12e8b6d9 unique.m: Set default to "first" when multiple options present.
Rik <rik@octave.org>
parents: 27235
diff changeset
129 endif
27291
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
130 if (! optsorted && ! optstable)
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
131 optsorted = true;
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
132 endif
27236
d29a12e8b6d9 unique.m: Set default to "first" when multiple options present.
Rik <rik@octave.org>
parents: 27235
diff changeset
133
19003
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18857
diff changeset
134 if (optrows && iscellstr (x))
10882
055b95863699 unique.m: Improve performance by only calculating necessary outputs.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
135 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
136 optrows = false;
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7650
diff changeset
137 endif
8412
970b4dbff9e4 optimize unique called with a single argument
Daniel J Sebald <daniel.sebald@ieee.org>
parents: 7920
diff changeset
138 else
19003
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18857
diff changeset
139 optrows = false;
27226
6eb32f0aea87 Implement "legacy" flag for intersect function (bug #56592).
Rik <rik@octave.org>
parents: 26376
diff changeset
140 optfirst = true;
27291
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
141 optsorted = true;
27226
6eb32f0aea87 Implement "legacy" flag for intersect function (bug #56592).
Rik <rik@octave.org>
parents: 26376
diff changeset
142 optlegacy = false;
7920
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7650
diff changeset
143 endif
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7650
diff changeset
144
18857
7bbe3658c5ef maint: Use "FIXME:" coding convention in m-files.
Rik <rik@octave.org>
parents: 17744
diff changeset
145 ## FIXME: The operations
10373
1f11fabfa349 improve performance of unique for sparse arrays
John W. Eaton <jwe@octave.org>
parents: 10372
diff changeset
146 ##
1f11fabfa349 improve performance of unique for sparse arrays
John W. Eaton <jwe@octave.org>
parents: 10372
diff changeset
147 ## 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
148 ## y(idx) = [];
1f11fabfa349 improve performance of unique for sparse arrays
John W. Eaton <jwe@octave.org>
parents: 10372
diff changeset
149 ##
1f11fabfa349 improve performance of unique for sparse arrays
John W. Eaton <jwe@octave.org>
parents: 10372
diff changeset
150 ## 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
151 ## 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
152 ## sparse array as long as we are not operating on rows.
11097
ffb2f1ef2097 make issparse, sparse, and spalloc built-in functions
John W. Eaton <jwe@octave.org>
parents: 10885
diff changeset
153 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
154 if (nnz (x) < numel (x))
19003
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18857
diff changeset
155 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
156 else
ffb2f1ef2097 make issparse, sparse, and spalloc built-in functions
John W. Eaton <jwe@octave.org>
parents: 10885
diff changeset
157 ## 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
158 y = unique (full (x), varargin{:});
10373
1f11fabfa349 improve performance of unique for sparse arrays
John W. Eaton <jwe@octave.org>
parents: 10372
diff changeset
159 endif
11097
ffb2f1ef2097 make issparse, sparse, and spalloc built-in functions
John W. Eaton <jwe@octave.org>
parents: 10885
diff changeset
160 return;
10373
1f11fabfa349 improve performance of unique for sparse arrays
John W. Eaton <jwe@octave.org>
parents: 10372
diff changeset
161 endif
1f11fabfa349 improve performance of unique for sparse arrays
John W. Eaton <jwe@octave.org>
parents: 10372
diff changeset
162
7920
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7650
diff changeset
163 if (optrows)
10882
055b95863699 unique.m: Improve performance by only calculating necessary outputs.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
164 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
165 isrowvec = false;
7920
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7650
diff changeset
166 else
6609
30891d1d0c86 [project @ 2007-05-09 02:12:04 by jwe]
jwe
parents: 6046
diff changeset
167 n = numel (x);
27226
6eb32f0aea87 Implement "legacy" flag for intersect function (bug #56592).
Rik <rik@octave.org>
parents: 26376
diff changeset
168 isrowvec = isrow (x);
5165
b822b4895af2 [project @ 2005-02-27 20:51:35 by dbateman]
dbateman
parents:
diff changeset
169 endif
b822b4895af2 [project @ 2005-02-27 20:51:35 by dbateman]
dbateman
parents:
diff changeset
170
10882
055b95863699 unique.m: Improve performance by only calculating necessary outputs.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
171 ## Special cases 0 and 1
055b95863699 unique.m: Improve performance by only calculating necessary outputs.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
172 if (n == 0)
27291
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
173 y = x;
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
174 if (! optrows && any (size (x)))
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
175 if (iscellstr (x))
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
176 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
177 else
27291
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
178 y = zeros (0, 1, class (x));
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
179 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
180 endif
5165
b822b4895af2 [project @ 2005-02-27 20:51:35 by dbateman]
dbateman
parents:
diff changeset
181 i = j = [];
5168
912058eb8360 [project @ 2005-02-28 03:23:36 by jwe]
jwe
parents: 5165
diff changeset
182 return;
10882
055b95863699 unique.m: Improve performance by only calculating necessary outputs.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
183 elseif (n == 1)
27291
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
184 y = x;
5165
b822b4895af2 [project @ 2005-02-27 20:51:35 by dbateman]
dbateman
parents:
diff changeset
185 i = j = 1;
5168
912058eb8360 [project @ 2005-02-28 03:23:36 by jwe]
jwe
parents: 5165
diff changeset
186 return;
5165
b822b4895af2 [project @ 2005-02-27 20:51:35 by dbateman]
dbateman
parents:
diff changeset
187 endif
b822b4895af2 [project @ 2005-02-27 20:51:35 by dbateman]
dbateman
parents:
diff changeset
188
27291
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
189 ## Calculate y output
7920
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7650
diff changeset
190 if (optrows)
27291
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
191 if (nargout > 1 || ! optsorted)
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
192 [y, i] = sortrows (x);
27226
6eb32f0aea87 Implement "legacy" flag for intersect function (bug #56592).
Rik <rik@octave.org>
parents: 26376
diff changeset
193 i = i(:);
10882
055b95863699 unique.m: Improve performance by only calculating necessary outputs.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
194 else
27291
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
195 y = sortrows (x);
10882
055b95863699 unique.m: Improve performance by only calculating necessary outputs.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
196 endif
7920
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7650
diff changeset
197 match = all (y(1:n-1,:) == y(2:n,:), 2);
27291
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
198 if (optsorted)
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
199 y(match,:) = [];
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
200 else
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
201 y = x;
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
202 y(i([false; match]), :) = [];
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
203 endif
5165
b822b4895af2 [project @ 2005-02-27 20:51:35 by dbateman]
dbateman
parents:
diff changeset
204 else
27291
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
205 if (isvector (x))
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
206 y = x;
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
207 else
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
208 y = x(:);
5168
912058eb8360 [project @ 2005-02-28 03:23:36 by jwe]
jwe
parents: 5165
diff changeset
209 endif
27291
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
210 if (nargout > 1 || ! optsorted)
10882
055b95863699 unique.m: Improve performance by only calculating necessary outputs.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
211 [y, i] = sort (y);
27226
6eb32f0aea87 Implement "legacy" flag for intersect function (bug #56592).
Rik <rik@octave.org>
parents: 26376
diff changeset
212 i = i(:);
10882
055b95863699 unique.m: Improve performance by only calculating necessary outputs.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
213 else
055b95863699 unique.m: Improve performance by only calculating necessary outputs.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
214 y = sort (y);
055b95863699 unique.m: Improve performance by only calculating necessary outputs.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
215 endif
19003
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18857
diff changeset
216 if (iscellstr (y))
7920
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7650
diff changeset
217 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
218 else
7920
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7650
diff changeset
219 match = (y(1:n-1) == y(2:n));
5205
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents: 5168
diff changeset
220 endif
27291
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
221 if (optsorted)
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
222 y(match) = [];
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
223 else
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
224 if (isvector (x))
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
225 y = x;
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
226 else
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
227 y = x(:);
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
228 endif
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
229 y(i([false; match(:)])) = [];
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
230 endif
5165
b822b4895af2 [project @ 2005-02-27 20:51:35 by dbateman]
dbateman
parents:
diff changeset
231 endif
b822b4895af2 [project @ 2005-02-27 20:51:35 by dbateman]
dbateman
parents:
diff changeset
232
27291
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
233 ## Calculate j output (3rd output)
10882
055b95863699 unique.m: Improve performance by only calculating necessary outputs.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
234 if (isargout (3))
27291
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
235 j = i; # cheap way to copy dimensions
27226
6eb32f0aea87 Implement "legacy" flag for intersect function (bug #56592).
Rik <rik@octave.org>
parents: 26376
diff changeset
236 j(i) = cumsum ([1; ! match(:)]);
27291
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
237 if (! optsorted)
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
238 warning ("unique: third output J is not yet implemented");
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
239 j = [];
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
240 endif
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
241
27226
6eb32f0aea87 Implement "legacy" flag for intersect function (bug #56592).
Rik <rik@octave.org>
parents: 26376
diff changeset
242 if (optlegacy && isrowvec)
6eb32f0aea87 Implement "legacy" flag for intersect function (bug #56592).
Rik <rik@octave.org>
parents: 26376
diff changeset
243 j = j.';
8610
85c9906abfd1 use endif and endfor instead of end
John W. Eaton <jwe@octave.org>
parents: 8502
diff changeset
244 endif
5165
b822b4895af2 [project @ 2005-02-27 20:51:35 by dbateman]
dbateman
parents:
diff changeset
245 endif
8412
970b4dbff9e4 optimize unique called with a single argument
Daniel J Sebald <daniel.sebald@ieee.org>
parents: 7920
diff changeset
246
27291
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
247 ## Calculate i output (2nd output)
10882
055b95863699 unique.m: Improve performance by only calculating necessary outputs.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
248 if (isargout (2))
27291
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
249 if (optsorted)
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
250 idx = find (match);
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
251 if (! optlegacy && optfirst)
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
252 idx += 1; # in-place is faster than other forms of increment
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
253 endif
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
254 i(idx) = [];
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
255 else
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
256 i([false; match(:)]) = [];
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
257 ## FIXME: Is there a way to avoid a call to sort?
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
258 i = sort (i);
10882
055b95863699 unique.m: Improve performance by only calculating necessary outputs.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
259 endif
27291
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
260
27226
6eb32f0aea87 Implement "legacy" flag for intersect function (bug #56592).
Rik <rik@octave.org>
parents: 26376
diff changeset
261 if (optlegacy && isrowvec)
6eb32f0aea87 Implement "legacy" flag for intersect function (bug #56592).
Rik <rik@octave.org>
parents: 26376
diff changeset
262 i = i.';
6eb32f0aea87 Implement "legacy" flag for intersect function (bug #56592).
Rik <rik@octave.org>
parents: 26376
diff changeset
263 endif
7920
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7650
diff changeset
264 endif
5165
b822b4895af2 [project @ 2005-02-27 20:51:35 by dbateman]
dbateman
parents:
diff changeset
265
b822b4895af2 [project @ 2005-02-27 20:51:35 by dbateman]
dbateman
parents:
diff changeset
266 endfunction
b822b4895af2 [project @ 2005-02-27 20:51:35 by dbateman]
dbateman
parents:
diff changeset
267
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
268
19003
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18857
diff changeset
269 %!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
270 %!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
271 %!assert (unique ([]), [])
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18857
diff changeset
272 %!assert (unique ([1]), [1])
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18857
diff changeset
273 %!assert (unique ([1 2]), [1 2])
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18857
diff changeset
274 %!assert (unique ([1;2]), [1;2])
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18857
diff changeset
275 %!assert (unique ([1,NaN,Inf,NaN,Inf]), [1,Inf,NaN,NaN])
27291
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
276 %!assert (unique ([1,NaN,Inf,NaN,Inf], "stable"), [1,NaN,Inf,NaN])
19003
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18857
diff changeset
277 %!assert (unique ({"Foo","Bar","Foo"}), {"Bar","Foo"})
27291
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
278 %!assert (unique ({"Foo","Bar","Foo"}, "stable"), {"Foo", "Bar"})
19003
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18857
diff changeset
279 %!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
280 %!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
281 %!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
282 %!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
283 %!assert (unique ({}), {})
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
284 %!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
285 %!assert (unique ([1,2,2,3,2,4]), [1,2,3,4])
27296
538468f901dd Clean up BIST tests in set functions.
Rik <rik@octave.org>
parents: 27291
diff changeset
286 %!assert (unique ([1,2,2,3,2,4]', "rows"), [1;2;3;4])
538468f901dd Clean up BIST tests in set functions.
Rik <rik@octave.org>
parents: 27291
diff changeset
287 %!assert (unique (sparse ([2,0;2,0])), [0;2])
538468f901dd Clean up BIST tests in set functions.
Rik <rik@octave.org>
parents: 27291
diff changeset
288 %!assert (unique (sparse ([1,2;2,3])), [1;2;3])
538468f901dd Clean up BIST tests in set functions.
Rik <rik@octave.org>
parents: 27291
diff changeset
289 %!assert (unique ([1,2,2,3,2,4]', "rows"), [1;2;3;4])
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
290 %!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
291 %!assert (unique (single ([1,2,2,3,2,4])), single ([1,2,3,4]))
27296
538468f901dd Clean up BIST tests in set functions.
Rik <rik@octave.org>
parents: 27291
diff changeset
292 %!assert (unique (single ([1,2,2,3,2,4]'), "rows"), single ([1;2;3;4]))
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
293 %!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
294 %!assert (unique (uint8 ([1,2,2,3,2,4])), uint8 ([1,2,3,4]))
27296
538468f901dd Clean up BIST tests in set functions.
Rik <rik@octave.org>
parents: 27291
diff changeset
295 %!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
296
27296
538468f901dd Clean up BIST tests in set functions.
Rik <rik@octave.org>
parents: 27291
diff changeset
297 ## Test options with numeric inputs
7920
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7650
diff changeset
298 %!test
27296
538468f901dd Clean up BIST tests in set functions.
Rik <rik@octave.org>
parents: 27291
diff changeset
299 %! [y,i,j] = unique ([1,1,2,3,3,3,4], "sorted");
27236
d29a12e8b6d9 unique.m: Set default to "first" when multiple options present.
Rik <rik@octave.org>
parents: 27235
diff changeset
300 %! assert (y, [1,2,3,4]);
27226
6eb32f0aea87 Implement "legacy" flag for intersect function (bug #56592).
Rik <rik@octave.org>
parents: 26376
diff changeset
301 %! assert (i, [1;3;4;7]);
6eb32f0aea87 Implement "legacy" flag for intersect function (bug #56592).
Rik <rik@octave.org>
parents: 26376
diff changeset
302 %! 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
303
7920
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7650
diff changeset
304 %!test
27291
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
305 %! [y,i,~] = unique ([4,4,2,2,2,3,1], "stable");
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
306 %! assert (y, [4,2,3,1]);
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
307 %! assert (i, [1;3;6;7]);
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
308 %! ##assert (j, []);
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
309
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
310 %!test
27236
d29a12e8b6d9 unique.m: Set default to "first" when multiple options present.
Rik <rik@octave.org>
parents: 27235
diff changeset
311 %! [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
312 %! assert (y, [1,2,3,4]');
27226
6eb32f0aea87 Implement "legacy" flag for intersect function (bug #56592).
Rik <rik@octave.org>
parents: 26376
diff changeset
313 %! assert (i, [2;3;6;7]);
6eb32f0aea87 Implement "legacy" flag for intersect function (bug #56592).
Rik <rik@octave.org>
parents: 26376
diff changeset
314 %! 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
315
27296
538468f901dd Clean up BIST tests in set functions.
Rik <rik@octave.org>
parents: 27291
diff changeset
316 ## Test options with cellstr inputs
8502
d437e8dc18fa make unique work for row vectors
Daniel J Sebald <daniel.sebald@ieee.org>
parents: 8413
diff changeset
317 %!test
27236
d29a12e8b6d9 unique.m: Set default to "first" when multiple options present.
Rik <rik@octave.org>
parents: 27235
diff changeset
318 %! [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
319 %! assert (y, {"z"});
27226
6eb32f0aea87 Implement "legacy" flag for intersect function (bug #56592).
Rik <rik@octave.org>
parents: 26376
diff changeset
320 %! assert (i, [1]);
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
321 %! assert (j, [1;1;1]);
27226
6eb32f0aea87 Implement "legacy" flag for intersect function (bug #56592).
Rik <rik@octave.org>
parents: 26376
diff changeset
322
8502
d437e8dc18fa make unique work for row vectors
Daniel J Sebald <daniel.sebald@ieee.org>
parents: 8413
diff changeset
323 %!test
27291
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
324 %! [y,i,~] = unique ({"B"; "A"; "B"}, "stable");
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
325 %! assert (y, {"B"; "A"});
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
326 %! assert (i, [1; 2]);
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
327
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
328 %!test
27226
6eb32f0aea87 Implement "legacy" flag for intersect function (bug #56592).
Rik <rik@octave.org>
parents: 26376
diff changeset
329 %! 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
330 %! [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
331 %! assert (y, [1,2,3]);
d29a12e8b6d9 unique.m: Set default to "first" when multiple options present.
Rik <rik@octave.org>
parents: 27235
diff changeset
332 %! assert (A(i,:), y);
d29a12e8b6d9 unique.m: Set default to "first" when multiple options present.
Rik <rik@octave.org>
parents: 27235
diff changeset
333 %! 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
334
27226
6eb32f0aea87 Implement "legacy" flag for intersect function (bug #56592).
Rik <rik@octave.org>
parents: 26376
diff changeset
335 %!test
27291
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
336 %! A = [4,5,6; 1,2,3; 4,5,6];
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
337 %! [y,i,~] = unique (A, "rows", "stable");
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
338 %! assert (y, [4,5,6; 1,2,3]);
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
339 %! assert (A(i,:), y);
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
340 %! ##assert (y(j,:), A);
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
341
27296
538468f901dd Clean up BIST tests in set functions.
Rik <rik@octave.org>
parents: 27291
diff changeset
342 ## Test "legacy" option
27291
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
343 %!test
27236
d29a12e8b6d9 unique.m: Set default to "first" when multiple options present.
Rik <rik@octave.org>
parents: 27235
diff changeset
344 %! [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
345 %! assert (y, [1,2,3,4]);
27226
6eb32f0aea87 Implement "legacy" flag for intersect function (bug #56592).
Rik <rik@octave.org>
parents: 26376
diff changeset
346 %! assert (i, [2,3,6,7]);
6eb32f0aea87 Implement "legacy" flag for intersect function (bug #56592).
Rik <rik@octave.org>
parents: 26376
diff changeset
347 %! 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
348
27228
c80681b4948d unique.m: Fix special case of "legacy" and "rows" flag both present.
Rik <rik@octave.org>
parents: 27226
diff changeset
349 %!test
27236
d29a12e8b6d9 unique.m: Set default to "first" when multiple options present.
Rik <rik@octave.org>
parents: 27235
diff changeset
350 %! 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
351 %! [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
352 %! 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
353 %! 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
354 %! 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
355
19833
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19700
diff changeset
356 ## Test input validation
28896
90fea9cc9caa test: Add expected error message <Invalid call> to BIST tests for nargin.
Rik <rik@octave.org>
parents: 27923
diff changeset
357 %!error <Invalid call> unique ()
19700
00e31f316a3a Fix Matlab incompatibility of "ismatrix" (bug #42422).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 19697
diff changeset
358 %!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
359 %!error <options must be strings> unique (1, 2)
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18857
diff changeset
360 %!error <cannot specify both "first" and "last"> unique (1, "first", "last")
27291
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
361 %!error <cannot specify both "sorted" and "stable">
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
362 %! unique (1, "sorted", "stable");
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
363 %!error <cannot specify "first"/"last" with "sorted"/"stable">
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
364 %! unique (1, "first", "sorted");
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
365 %!error <cannot specify "first"/"last" with "sorted"/"stable">
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
366 %! unique (1, "last", "stable");
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
367 %!error <cannot specify "sorted" or "stable" with "legacy">
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
368 %! unique (1, "sorted", "legacy");
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
369 %!error <cannot specify "sorted" or "stable" with "legacy">
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
370 %! unique (1, "stable", "legacy");
19003
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18857
diff changeset
371 %!error <invalid option> unique (1, "middle")
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18857
diff changeset
372 %!error <invalid option> unique ({"a", "b", "c"}, "UnknownOption")
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18857
diff changeset
373 %!error <invalid option> unique ({"a", "b", "c"}, "UnknownOption1", "UnknownOption2")
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18857
diff changeset
374 %!error <invalid option> unique ({"a", "b", "c"}, "rows", "UnknownOption2")
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18857
diff changeset
375 %!error <invalid option> unique ({"a", "b", "c"}, "UnknownOption1", "last")
d00f6b09258f Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents: 18857
diff changeset
376 %!warning <"rows" is ignored for cell arrays> unique ({"1"}, "rows");
27291
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
377 %!warning <third output J is not yet implemented>
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
378 %! [y,i,j] = unique ([2,1], "stable");
c4f9a0f097a3 unique.m: Add new option "stable" to control output ordering.
Rik <rik@octave.org>
parents: 27236
diff changeset
379 %! assert (j, []);