annotate scripts/set/uniquetol.m @ 29463:09767c20dec9

uniquetol.m: variable naming cleanup and addition of BIST test. * uniquetol.m: Rename some variables for clarity. Add FIXME note about compatibility of orientation of output. Add BIST test for Matlab compatibility of vectors.
author Rik <rik@octave.org>
date Wed, 24 Mar 2021 19:56:42 -0700
parents df641f946202
children d6415c931759
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
1 ########################################################################
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
2 ##
29359
7854d5752dd2 maint: merge stable to default.
John W. Eaton <jwe@octave.org>
parents: 29312
diff changeset
3 ## Copyright (C) 2020-2021 The Octave Project Developers
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
4 ##
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
5 ## See the file COPYRIGHT.md in the top-level directory of this
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
6 ## distribution or <https://octave.org/copyright/>.
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
7 ##
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
8 ## This file is part of Octave.
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
9 ##
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
10 ## Octave is free software: you can redistribute it and/or modify it
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
11 ## under the terms of the GNU General Public License as published by
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
12 ## the Free Software Foundation, either version 3 of the License, or
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
13 ## (at your option) any later version.
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
14 ##
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
15 ## Octave is distributed in the hope that it will be useful, but
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
18 ## GNU General Public License for more details.
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
19 ##
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
20 ## You should have received a copy of the GNU General Public License
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
21 ## along with Octave; see the file COPYING. If not, see
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
22 ## <https://www.gnu.org/licenses/>.
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
23 ##
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
24 ########################################################################
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
25
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
26 ## -*- texinfo -*-
29309
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
27 ## @deftypefn {} {@var{c} =} uniquetol (@var{A})
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
28 ## @deftypefnx {} {@var{c} =} uniquetol (@var{A}, @var{tol})
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
29 ## @deftypefnx {} {@var{c} =} uniquetol (@dots{}, @var{property}, @var{value})
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
30 ## @deftypefnx {} {[@var{c}, @var{ia}, @var{ic}] =} uniquetol (@dots{})
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
31 ## Return the unique elements of @var{A} within tolerance @var{tol}.
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
32 ##
29311
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
33 ## Two values, @var{x} and @var{y}, are within relative tolerance if
29309
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
34 ## @code{abs (@var{x} - @var{y}) <= @var{tol} * max (abs (@var{A}(:)))}.
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
35 ##
29311
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
36 ## The input @var{A} must be a floating point type (double or single).
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
37 ##
29311
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
38 ## If @var{tol} is unspecified, the default tolerance is 1e-12 for double
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
39 ## precision input or 1e-6 for single precision input.
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
40 ##
29311
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
41 ## The function may also be called with the following optional property/value
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
42 ## pairs. Property/value pairs must be passed after other input arguments:
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
43 ##
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
44 ## @table @asis
29311
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
45 ## @item @qcode{"ByRows"} (default: @code{false})
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
46 ## When true, return the unique rows of @var{A}. @var{A} must be a 2-D array
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
47 ## to use this option. For rows, the criteria for uniqueness is changed to
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
48 ## @code{all (abs (@var{x} - @var{y}) <= @var{tol}*max (abs (@var{A}),[],1))}
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
49 ## which compares each column component of a row against a column-specific
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
50 ## tolerance.
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
51 ##
29311
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
52 ## @item @qcode{"DataScale"}
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
53 ## The tolerance test is changed to
29311
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
54 ## @code{abs (@var{x} - @var{y}) <= @var{tol}*@var{DS}} where @var{DS} is a
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
55 ## scalar unless the property @qcode{"ByRows"} is true. In that case, @var{DS}
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
56 ## can either be a scalar or a vector with a length equal to the number of
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
57 ## columns in @var{A}. Using a value of @code{1.0} for @var{DS} will change
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
58 ## the tolerance from a relative one to an absolute tolerance. Using a value
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
59 ## of @code{Inf} will disable testing.
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
60 ##
29311
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
61 ## @item @qcode{"OutputAllIndices"} (default: @code{false})
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
62 ## When true, @var{ia} is a cell array (not a vector) that contains the indices
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
63 ## for @emph{all} elements in @var{A} that are within tolerance of a value in
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
64 ## @var{C}. That is, each cell in @var{ia} corresponds to a single unique
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
65 ## value in @var{C}, and the values in each cell correspond to locations in
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
66 ## @var{A}.
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
67 ## @end table
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
68 ##
29311
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
69 ## The output @var{c} is a row vector if the input @var{A} is a row vector.
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
70 ## For all other cases, a column vector is returned.
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
71 ##
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
72 ## The optional output @var{ia} is a column index vector such that
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
73 ## @code{@var{c} = @var{A}(@var{ia})}. If the @qcode{"ByRows"} property is
29312
875d799ab0b3 uniquetol.m: Fix texinfo syntax errors (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents: 29311
diff changeset
74 ## true, the condition is @code{@var{c} = @var{A}(@var{ia}, :)}. If the
875d799ab0b3 uniquetol.m: Fix texinfo syntax errors (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents: 29311
diff changeset
75 ## @qcode{"OutputAllIndices"} property is true, then the values
875d799ab0b3 uniquetol.m: Fix texinfo syntax errors (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents: 29311
diff changeset
76 ## @code{@var{A}(@var{ia}@{@var{i}@})} are all within tolerance of the unique
875d799ab0b3 uniquetol.m: Fix texinfo syntax errors (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents: 29311
diff changeset
77 ## value @code{@var{c}(@var{i})}.
29311
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
78 ##
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
79 ## The optional output @var{ic} is a column index vector such that
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
80 ## @code{@var{A} = @var{c}(@var{ic})} when @var{A} is a vector. When @var{A}
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
81 ## is a matrix, @code{@var{A}(:) = @var{c}(@var{ic})}. If the @qcode{"ByRows"}
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
82 ## property is true then @code{@var{A} = @var{c}(@var{ic},:)}.
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
83 ##
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
84 ## Example: small round-off errors require @code{uniquetol}, not @code{unique}
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
85 ##
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
86 ## @example
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
87 ## @group
29311
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
88 ## x = [1:5];
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
89 ## ## Inverse_Function (Function (x)) should return exactly x
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
90 ## y = exp (log (x));
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
91 ## D = unique ([x, y])
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
92 ## @result{} [1.0000 2.0000 3.0000 3.0000 4.0000 5.0000 5.0000]
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
93 ## C = uniquetol ([x, y])
29311
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
94 ## @result{} [1 2 3 4 5]
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
95 ## @end group
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
96 ## @end example
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
97 ##
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
98 ## @seealso{unique, union, intersect, setdiff, setxor, ismember}
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
99 ## @end deftypefn
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
100
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
101
29309
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
102 function [c, ia, ic] = uniquetol (A, varargin)
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
103
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
104 if (nargin < 1)
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
105 print_usage ();
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
106 endif
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
107
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
108 if (isempty (A))
29309
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
109 c = A;
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
110 ia = [];
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
111 ic = [];
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
112 return;
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
113 endif
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
114
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
115 if (! isfloat (A))
29309
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
116 error ("Octave:uniquetol:unsupported-type",
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
117 "uniquetol: A must be a double or single precision array");
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
118 endif
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
119
29309
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
120 if (nargin == 1 || ischar (varargin{1}))
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
121 tol = ifelse (isa (A, "double"), 1e-12, 1e-6);
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
122 elseif (! (isfloat (varargin{1}) && isscalar (varargin{1})))
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
123 error ("Octave:uniquetol:unsupported-type",
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
124 "uniquetol: TOL must be a double or single precision scalar");
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
125 else
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
126 tol = varargin{1};
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
127 varargin(1) = [];
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
128 endif
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
129
29309
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
130 if (mod (numel (varargin), 2))
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
131 error ("uniquetol: PROPERTY/VALUE arguments must be passed in pairs");
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
132 endif
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
133
29309
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
134 by_rows = false;
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
135 output_all_indices = false;
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
136 data_scale = [];
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
137
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
138 for k = 1:2:numel (varargin)
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
139 if (! ischar (varargin{k}))
29309
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
140 error ("uniquetol: PROPERTY must be a string");
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
141 endif
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
142
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
143 if (strcmpi (varargin{k}, "ByRows"))
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
144 by_rows = logical (varargin{k+1});
29309
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
145 if (by_rows && ndims (A) > 2)
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
146 error ('uniquetol: A must be a 2-D array when "ByRows" is true');
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
147 endif
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
148 elseif (strcmpi (varargin{k}, "OutputAllIndices"))
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
149 output_all_indices = logical (varargin{k+1});
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
150 elseif (strcmpi (varargin{k}, "DataScale"))
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
151 data_scale = varargin{k+1}(:).';
29309
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
152 if (! isfloat (data_scale) || any (data_scale(:) < 0)
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
153 || any (isnan (data_scale(:))))
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
154 error ("uniquetol: DataScale must be a non-NaN, positive floating point scalar or vector");
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
155 endif
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
156 cols_data_scale = columns (data_scale);
29309
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
157 if (cols_data_scale != 1 && cols_data_scale != columns (A))
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
158 error ("uniquetol: invalid DataScale size");
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
159 endif
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
160 else
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
161 error ("uniquetol: unknown property '%s'", varargin{k});
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
162 endif
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
163 endfor
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
164
29309
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
165 if (isempty (data_scale))
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
166 data_scale = max (abs (A(! isinf (A))(:)));
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
167 endif
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
168
29463
09767c20dec9 uniquetol.m: variable naming cleanup and addition of BIST test.
Rik <rik@octave.org>
parents: 29461
diff changeset
169 tol *= data_scale;
29461
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
170
29309
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
171 if (by_rows)
29461
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
172
29463
09767c20dec9 uniquetol.m: variable naming cleanup and addition of BIST test.
Rik <rik@octave.org>
parents: 29461
diff changeset
173 nr = rows (A);
09767c20dec9 uniquetol.m: variable naming cleanup and addition of BIST test.
Rik <rik@octave.org>
parents: 29461
diff changeset
174 nc = columns (A);
09767c20dec9 uniquetol.m: variable naming cleanup and addition of BIST test.
Rik <rik@octave.org>
parents: 29461
diff changeset
175 Iall = zeros (nr, 1);
09767c20dec9 uniquetol.m: variable naming cleanup and addition of BIST test.
Rik <rik@octave.org>
parents: 29461
diff changeset
176 I = NaN (nc, 1);
29461
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
177 ia = {};
29463
09767c20dec9 uniquetol.m: variable naming cleanup and addition of BIST test.
Rik <rik@octave.org>
parents: 29461
diff changeset
178 J = NaN (nc, 1);
29461
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
179 j = 1;
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
180 ii = 0;
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
181
29463
09767c20dec9 uniquetol.m: variable naming cleanup and addition of BIST test.
Rik <rik@octave.org>
parents: 29461
diff changeset
182 for i = 1:nr
29461
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
183 if (any (Iall == i))
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
184 continue;
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
185 else
29463
09767c20dec9 uniquetol.m: variable naming cleanup and addition of BIST test.
Rik <rik@octave.org>
parents: 29461
diff changeset
186 equ = all (abs (A - A(i,:)) <= tol, 2);
29461
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
187 equ(i,1) = equ(i,1) || any (! isfinite (A(i,:)), 2);
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
188 sumeq = sum (equ);
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
189 ia_tmp = find (equ);
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
190 if (output_all_indices)
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
191 ia{end+1} = ia_tmp;
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
192 endif
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
193 Iall(ii+(1:sumeq)) = ia_tmp;
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
194 I(j) = ia_tmp(1);
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
195 J(equ) = j;
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
196 ii += sumeq;
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
197 j += 1;
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
198 endif
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
199 endfor
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
200
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
201 I(isnan (I)) = [];
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
202 J(isnan (J)) = [];
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
203 c = A(I,:);
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
204
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
205 if (! output_all_indices)
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
206 ia = I(1:j-1);
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
207 endif
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
208 ic = J;
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
209
29309
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
210 else
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
211 isrowvec = isrow (A);
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
212 A = A(:);
29463
09767c20dec9 uniquetol.m: variable naming cleanup and addition of BIST test.
Rik <rik@octave.org>
parents: 29461
diff changeset
213 nr = rows (A);
29461
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
214 isnanA = isnan (A);
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
215 anyisnanA = any (isnanA);
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
216 [sortA, sAi] = sort (A);
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
217 diffsortA = diff (sortA);
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
218 isinfsortA = isinf (sortA);
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
219 isnansortA = isnan (sortA);
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
220 numnan = sum (isnansortA);
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
221 if (any (isinfsortA))
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
222 sAnin = sortA(! (isinfsortA | isnansortA));
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
223 diffsortA(isinf (diffsortA)) = abs (sAnin(end) - sAnin(1)) + 10;
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
224 endif
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
225 csdx = cumsum (diffsortA);
29463
09767c20dec9 uniquetol.m: variable naming cleanup and addition of BIST test.
Rik <rik@octave.org>
parents: 29461
diff changeset
226 ue = [true; diff([0; csdx-mod(csdx,tol)]) > eps(max(csdx))];
29461
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
227 ueold = NaN;
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
228 while (any (ueold != ue))
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
229 ueold = ue;
29463
09767c20dec9 uniquetol.m: variable naming cleanup and addition of BIST test.
Rik <rik@octave.org>
parents: 29461
diff changeset
230 belowtol = [false; diff(sortA(ue)) < tol];
29461
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
231 if (any (belowtol))
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
232 needstomove = find (ue)(belowtol);
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
233 ue(needstomove) = false;
29463
09767c20dec9 uniquetol.m: variable naming cleanup and addition of BIST test.
Rik <rik@octave.org>
parents: 29461
diff changeset
234 needstomove(needstomove >= nr-numnan) = [];
29461
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
235 ue(needstomove+1) = true;
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
236 endif
29461
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
237 endwhile
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
238 c = sortA(ue);
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
239 [~, sortsAi] = sort (sAi);
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
240 cumsumue = cumsum (ue);
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
241 ic = cumsumue(sortsAi);
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
242 if (anyisnanA)
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
243 findisnanA = find (isnanA);
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
244 else
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
245 findisnanA = [];
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
246 endif
29461
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
247 if (output_all_indices)
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
248 nu = cumsumue(end);
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
249 ia = cell (1, nu);
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
250 for k = 1:nu
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
251 ia{k} = setdiff (sAi(cumsumue==k), findisnanA);
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
252 endfor
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
253 else
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
254 ia = sAi(ue);
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
255 endif
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
256
29461
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
257 if (anyisnanA)
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
258 rowsc1 = rows (c) + (1:sum (isnanA));
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
259 c(rowsc1) = NaN;
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
260 ia(rowsc1) = findisnanA;
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
261 ic(isnanA) = rowsc1;
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
262 endif
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
263
29463
09767c20dec9 uniquetol.m: variable naming cleanup and addition of BIST test.
Rik <rik@octave.org>
parents: 29461
diff changeset
264 ## FIXME: Matlab-compatible orientation of output
09767c20dec9 uniquetol.m: variable naming cleanup and addition of BIST test.
Rik <rik@octave.org>
parents: 29461
diff changeset
265 ## Actually, Matlab prefers row vectors (2021/03/24), but this is different
09767c20dec9 uniquetol.m: variable naming cleanup and addition of BIST test.
Rik <rik@octave.org>
parents: 29461
diff changeset
266 ## from all the other set functions which prefer column vectors. Assume
09767c20dec9 uniquetol.m: variable naming cleanup and addition of BIST test.
Rik <rik@octave.org>
parents: 29461
diff changeset
267 ## that this is a bug in Matlab's implementation and prefer column vectors.
29461
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
268 if (isrowvec)
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
269 c = c.';
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
270 endif
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
271
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
272 endif
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
273
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
274 endfunction
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
275
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
276
29311
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
277 %!assert (uniquetol ([1 1 2; 1 2 1; 1 1 2+10*eps]), [1;2])
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
278 %!assert (uniquetol ([1 1 2; 1 0 1; 1 1 2+10*eps], "byrows", true),
29309
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
279 %! [1 1 2; 1 0 1])
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
280 %!assert (uniquetol ([]), [])
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
281 %!assert (uniquetol ([1]), [1])
29461
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
282 %!assert (uniquetol ([2, 1]), [1, 2]);
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
283 %!assert (uniquetol ([1; 2]), [1; 2])
29461
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
284 %!assert (uniquetol ([-Inf, 1, NaN, Inf, NaN, Inf]), [-Inf, 1, Inf, NaN, NaN]);
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
285 %!assert (uniquetol (zeros (1, 0)), zeros (1, 0));
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
286 %!assert (uniquetol (zeros (1, 0), "byrows", true), zeros (1, 0))
29311
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
287 %!assert (uniquetol ([1,2,2,3,2,4], "byrows", true), [1,2,2,3,2,4])
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
288 %!assert (uniquetol ([1,2,2,3,2,4]), [1,2,3,4])
29311
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
289 %!assert (uniquetol ([1,2,2,3,2,4].', "byrows", true), [1;2;3;4])
29461
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
290 %!assert (uniquetol (sparse ([2,0;2,0])), sparse ([0;2]))
29309
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
291 %!assert (uniquetol (sparse ([1,2;2,3])), sparse ([1;2;3]))
29311
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
292 %!assert (uniquetol (single ([1,2,2,3,2,4]), "byrows", true),
29309
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
293 %! single ([1,2,2,3,2,4]))
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
294 %!assert (uniquetol (single ([1,2,2,3,2,4])), single ([1,2,3,4]))
29311
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
295 %!assert (uniquetol (single ([1,2,2,3,2,4].'), "byrows", true),
29309
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
296 %! single ([1;2;3;4]))
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
297
29463
09767c20dec9 uniquetol.m: variable naming cleanup and addition of BIST test.
Rik <rik@octave.org>
parents: 29461
diff changeset
298 ## Matlab compatibility of output
09767c20dec9 uniquetol.m: variable naming cleanup and addition of BIST test.
Rik <rik@octave.org>
parents: 29461
diff changeset
299 %!test
09767c20dec9 uniquetol.m: variable naming cleanup and addition of BIST test.
Rik <rik@octave.org>
parents: 29461
diff changeset
300 %! x = 1:0.045:3;
09767c20dec9 uniquetol.m: variable naming cleanup and addition of BIST test.
Rik <rik@octave.org>
parents: 29461
diff changeset
301 %! y = uniquetol (x, 0.1, "datascale", 1);
09767c20dec9 uniquetol.m: variable naming cleanup and addition of BIST test.
Rik <rik@octave.org>
parents: 29461
diff changeset
302 %! assert (y(1:4), [1, 1.135, 1.27, 1.405]);
09767c20dec9 uniquetol.m: variable naming cleanup and addition of BIST test.
Rik <rik@octave.org>
parents: 29461
diff changeset
303
29311
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
304 ## Test index vector return arguments
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
305 %!test
29309
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
306 %! [c, ia, ic] = uniquetol ([1,1,2,3,3,3,4]);
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
307 %! assert (c, [1,2,3,4]);
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
308 %! assert (ia, [1;3;4;7]);
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
309 %! assert (ic, [1;1;2;3;3;3;4]);
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
310
29311
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
311 ## Test index vector return arguments with "ByRows"
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
312 %!test
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
313 %! A = [2, 3, 4; 2, 3, 4];
29309
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
314 %! [c, ia, ic] = uniquetol (A, "byrows", true);
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
315 %! assert (c, [2, 3, 4]);
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
316 %! assert (A(ia,:), c);
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
317 %! assert (c(ic,:), A);
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
318
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
319 %!test
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
320 %! x = (2:7)'*pi;
29311
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
321 %! y = exp (log (x));
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
322 %! C = uniquetol ([x; y]);
29461
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
323 %! assert (C, x, 1e-12);
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
324
29309
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
325 ## Test "ByRows" Property
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
326 %!test
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
327 %! A = [0.06, 0.21, 0.38; 0.38, 0.21, 0.39; 0.54, 0.56, 0.41; 0.46, 0.52, 0.95];
29311
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
328 %! B = log (exp (A));
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
329 %! C = uniquetol ([A; B], "ByRows", true);
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
330 %! assert (C, A);
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
331
29311
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
332 ## Test "DataScale" Property
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
333 %!test
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
334 %! x = 10^11;
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
335 %! C = uniquetol ([x, exp(log(x))], 1e-6, "DataScale", 1);
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
336 %! assert (C, [x, exp(log(x))]);
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
337
29309
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
338 ## Test "OutputAllIndices" Property
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
339 %!test
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
340 %! A = [.1 .2 .3 10];
29309
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
341 %! [C, ia, ic] = uniquetol (A, .1, "OutputAllIndices", true);
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
342 %! assert (C, [.1, 10]);
29309
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
343 %! assert (ia, {(1:3)', 4});
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
344 %! assert (ic, [1; 1; 1; 2]);
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
345
29309
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
346 ## Test input validation
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
347 %!error <Invalid call> uniquetol ()
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
348 %!error <A must be a double or single precision array> uniquetol (int8 (1))
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
349 %!error <TOL must be a double .* precision> uniquetol (1, int8 (1))
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
350 %!error <TOL must be a .* scalar> uniquetol (1, [1, 2])
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
351 %!error <arguments must be passed in pairs> uniquetol (1, 2, "byrows")
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
352 %!error <PROPERTY must be a string> uniquetol (1, 2, 3, "bar")
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
353 %!error <A must be a 2-D array> uniquetol (ones(2,2,2), "byrows", true)
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
354 %!error <DataScale must be a .* floating point> uniquetol (1, "DataScale", '1')
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
355 %!error <DataScale must be a .* positive> uniquetol (1, "DataScale", -1)
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
356 %!error <DataScale must be a non-NaN> uniquetol (1, "DataScale", NaN)
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
357 %!error <invalid DataScale size> uniquetol (1, "DataScale", [1 2])
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
358 %!error <unknown property 'foo'> uniquetol (1, "foo", "bar")
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
359 %!error <unknown property 'foo'> uniquetol (1, 2, "foo", "bar")