annotate scripts/statistics/normalize.m @ 31379:84fa33608593

normalize.m: Use bsxfun rather than broadcasting (bug #55765) * normalize.m: Replace broadcasting with bsxfun for in "range" option and primary calculation. Add FIXME note that it can revert to broadcasting when sparse and diagonal matrix types no longer produce errors. Add BISTs for sparse and diagonal input handling and xtests for preserving sparseness.
author Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
date Mon, 31 Oct 2022 16:25:46 -0400
parents d727bda73574
children fd29c7a50a78
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
1 ########################################################################
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
2 ##
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
3 ## Copyright (C) 2017-2022 The Octave Project Developers
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
4 ##
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
5 ## See the file COPYRIGHT.md in the top-level directory of this
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
6 ## distribution or <https://octave.org/copyright/>.
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
7 ##
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
8 ## This file is part of Octave.
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
9 ##
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
10 ## Octave is free software: you can redistribute it and/or modify it
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
11 ## under the terms of the GNU General Public License as published by
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
12 ## the Free Software Foundation, either version 3 of the License, or
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
13 ## (at your option) any later version.
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
14 ##
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
15 ## Octave is distributed in the hope that it will be useful, but
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
18 ## GNU General Public License for more details.
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
19 ##
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
20 ## You should have received a copy of the GNU General Public License
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
21 ## along with Octave; see the file COPYING. If not, see
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
22 ## <https://www.gnu.org/licenses/>.
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
23 ##
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
24 ########################################################################
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
25
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
26 ## -*- texinfo -*-
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
27 ## @deftypefn {} {@var{z} =} normalize (@var{x})
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
28 ## @deftypefnx {} {@var{z} =} normalize (@var{x}, @var{dim})
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
29 ## @deftypefnx {} {@var{z} =} normalize (@dots{}, @var{method})
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
30 ## @deftypefnx {} {@var{z} =} normalize (@dots{}, @var{method}, @var{option})
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
31 ## @deftypefnx {} {@var{z} =} normalize (@dots{}, @var{scale}, @var{scaleoption}, @var{center}, @var{centeroption})
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
32 ## @deftypefnx {} {[@var{z}, @var{c}, @var{s}] =} normalize (@dots{})
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
33 ##
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
34 ## Return a normalization of the data in @var{x} using one of several available
31355
4c6c8f14766c doc: Add normalize to manual and clean up docstring
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 31353
diff changeset
35 ## scaling and centering methods.
4c6c8f14766c doc: Add normalize to manual and clean up docstring
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 31353
diff changeset
36 ##
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
37 ## @code{normalize} by default will return the @code{zscore} of @var{x},
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
38 ## defined as the number of standard deviations each element is from the mean
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
39 ## of @var{x}. This is equivalent to centering at the mean of the data and
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
40 ## scaling by the standard deviation.
31355
4c6c8f14766c doc: Add normalize to manual and clean up docstring
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 31353
diff changeset
41 ##
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
42 ## The returned value @var{z} will have the same size as @var{x}. The optional
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
43 ## return variables @var{c} and @var{s} are the centering and scaling factors
31355
4c6c8f14766c doc: Add normalize to manual and clean up docstring
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 31353
diff changeset
44 ## used in the normalization such that:
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
45 ##
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
46 ## @example
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
47 ## @group
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
48 ## @tcode{@var{z} = (@var{x} - @var{c}) ./ @var{s}}
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
49 ## @end group
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
50 ## @end example
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
51 ##
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
52 ## If @var{x} is a vector, @code{normalize} will operate on the data in
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
53 ## @var{x}.
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
54 ##
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
55 ## If @var{x} is a matrix, @code{normalize} will operate independently on
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
56 ## each column in @var{x}.
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
57 ##
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
58 ## If @var{x} is an N-dimensional array, @code{normalize} will operate
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
59 ## independently on the first non-singleton dimension in @var{x}.
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
60 ##
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
61 ## If the optional second argument @var{dim} is given, operate along this
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
62 ## dimension.
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
63 ##
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
64 ## The optional inputs @var{method} and @var{option} can be used to specify the
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
65 ## type of normalization performed on @var{x}. Note that only the
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
66 ## @option{scale} and @option{center} options may be specified together using
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
67 ## any of the methods defined below. Valid normalization methods are:
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
68 ##
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
69 ## @table @code
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
70 ## @item zscore
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
71 ## (Default) Normalizes the elements in @var{x} to the scaled distance from a
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
72 ## central value. Valid Options:
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
73 ##
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
74 ## @table @code
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
75 ## @item std
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
76 ## (Default) Data is centered at @code{mean (@var{x})} and scaled by the
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
77 ## standard deviation.
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
78 ##
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
79 ## @item robust
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
80 ## Data is centered at @code{median (@var{x})} and scaled by the median
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
81 ## absolute deviation.
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
82 ## @end table
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
83 ##
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
84 ## @item norm
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
85 ## @var{z} is the general vector norm of @var{x}, with @var{option} being the
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
86 ## normalization factor @var{p} that determines the vector norm type according
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
87 ## to:
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
88 ## @tex
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
89 ## $$Z = \left (\sum_k \left | X_k \right |^P \right )^{1/P}$$
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
90 ## @end tex
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
91 ## @ifnottex
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
92 ##
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
93 ## @example
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
94 ## @group
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
95 ## @tcode{@var{z} = [sum (abs (@var{x}) .^ @var{p})] ^ (1/@var{p})}
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
96 ## @end group
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
97 ## @end example
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
98 ##
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
99 ## @end ifnottex
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
100 ## @var{p} can be any positive scalar, specific values being:
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
101 ##
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
102 ## @table @code
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
103 ## @item @var{p} = 1
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
104 ## @var{x} is normalized by @code{sum (abs (@var{x}))}.
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
105 ##
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
106 ## @item @var{p} = 2
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
107 ## (Default) @var{x} is normalized by the Euclidian norm, or vector
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
108 ## magnitude, of the elements.
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
109 ##
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
110 ## @item @var{P} = Inf
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
111 ## @var{x} is normalized by @code{max (abs (@var{x}))}.
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
112 ## @end table
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
113 ##
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
114 ## @item scale
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
115 ## @var{x} is scaled by a factor determined by @var{option}, which can be a
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
116 ## numeric scalar or one of the following:
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
117 ##
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
118 ## @table @code
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
119 ## @item std
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
120 ## (Default) @var{x} is scaled by its standard deviation.
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
121 ##
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
122 ## @item mad
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
123 ## @var{x} is scaled by its median absolute deviation.
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
124 ##
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
125 ## @item first
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
126 ## @var{x} is scaled by its first element.
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
127 ##
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
128 ## @item iqr
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
129 ## @var{x} is scaled by its interquartile range.
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
130 ## @end table
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
131 ##
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
132 ## @item range
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
133 ## @var{x} is scaled to fit the range specified by @var{option} as a two
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
134 ## element scalar row vector. The default range is [0, 1].
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
135 ##
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
136 ## @item center
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
137 ## @var{x} is shifted by an amount determined by @var{option}, which can be a
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
138 ## numeric scalar or one of the following:
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
139 ##
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
140 ## @table @code
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
141 ## @item mean
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
142 ## (Default) @var{x} is shifted by @code{mean (@var{x})}.
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
143 ##
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
144 ## @item median
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
145 ## @var{x} is shifted by @code{median (@var{x})}.
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
146 ## @end table
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
147 ##
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
148 ## @item medianiqr
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
149 ## @var{x} is shifted by @code{median (@var{x})} and scaled by the
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
150 ## interquartile range.
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
151 ## @end table
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
152 ##
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
153 ## Known @sc{matlab} incompatibilities:
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
154 ##
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
155 ## @enumerate
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
156 ## @item
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
157 ## The option @option{DataVariables} is not yet implemented for Table class
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
158 ## @var{x} inputs.
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
159 ##
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
160 ## @item
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
161 ## Certain arrays containing NaN elements may not return @sc{matlab} compatible
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
162 ## output.
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
163 ## @end enumerate
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
164 ##
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
165 ## @seealso{zscore, iqr, norm, rescale, std, median, mean, mad}
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
166 ## @end deftypefn
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
167
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
168 function [z, c, s] = normalize (x, varargin)
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
169
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
170 ## FIXME: Until NANFLAG/OMITNAN option is implemented in std, mean, median,
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
171 ## etc., normalize cannot efficiently reproduce some behavior with NaNs in
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
172 ## x. xtests added to capture this. (See bug #50571)
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
173
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
174 ## FIXME: When table class is implemented, remove DataVariables error line in
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
175 ## option checking section and add DataVariables data handling switch
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
176 ## section.
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
177
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
178 ## Input validation
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
179 if (nargin < 1 || nargin > 8)
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
180 print_usage ();
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
181 endif
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
182
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
183 if (! isnumeric (x))
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
184 error ("normalize: X must be a numeric vector, matrix, or array");
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
185 endif
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
186
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
187 if (nargin == 1)
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
188 ## Directly handle simple 1 input case.
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
189 [s, c] = std (x);
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
190
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
191 else
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
192 ## Parse input options
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
193 dim = [];
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
194 method = [];
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
195 methodoption = [];
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
196 datavariables_flag = false;
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
197 datavar = [];
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
198 scale_and_center_flag = false;
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
199
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
200 vararg_idx = 1;
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
201 ## Only second optional input can be numeric without following a method.
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
202 if (isnumeric (varargin{1}))
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
203 dim = varargin{1};
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
204 ## Check for valid dimensions
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
205 if (! (isscalar (dim) && dim == fix (dim) && dim > 0))
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
206 error ("normalize: DIM must be an integer and a valid dimension");
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
207 endif
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
208 vararg_idx++;
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
209 endif
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
210
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
211 ## Parse varargin to determine methods then options.
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
212 n_varargin = nargin - 1;
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
213 while (vararg_idx <= n_varargin)
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
214 ## Arguments after second cannot be numeric without following a method.
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
215 if (isnumeric (varargin{vararg_idx}))
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
216 print_usage ();
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
217 endif
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
218
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
219 prop = tolower (varargin{vararg_idx});
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
220
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
221 if (strcmp (prop, "datavariables"))
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
222 ## FIXME: Remove error on next line and undo block comment when support
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
223 ## for Tables is implemented.
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
224 error ("normalize: DataVariables method not yet implemented");
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
225 #{
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
226 if (vararg_idx == n_varargin)
31353
fa3ae8fd8449 normalize.m: correct error messages split across lines (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 31352
diff changeset
227 error (["normalize: DataVariables requires a table variable", ...
fa3ae8fd8449 normalize.m: correct error messages split across lines (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 31352
diff changeset
228 " be specified"]);
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
229 elseif (datavariables_flag == true)
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
230 error ("normalize: DataVariables may only be specified once");
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
231 else
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
232 datavariables_flag = true;
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
233 datavar = varargin{vararg_idx+1};
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
234 vararg_idx++;
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
235 endif
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
236 #}
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
237
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
238 else
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
239 if (! isempty (method))
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
240 ## Catch if a second method is passed
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
241 if (scale_and_center_flag)
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
242 ## if true, already specified two methods, three never possible
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
243 error ("normalize: more than two methods specified");
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
244
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
245 elseif (strcmp ({method, prop}, {"center", "scale"})
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
246 || strcmp ({method, prop}, {"scale", "center"}))
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
247 ## Only scale and center can be called together
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
248 scale_and_center_flag = true;
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
249 ## scale/center order doesn't matter, avoid overwriting first one
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
250 stored_method = method;
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
251 method = [];
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
252 stored_methodoption = methodoption;
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
253 methodoption = [];
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
254 else
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
255 ## not scale and center, throw appropriate error
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
256 if (any (strcmp (prop, {"zscore", "norm", "range", "scale", ...
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
257 "center", "medianiqr"})))
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
258 error ("normalize: methods '%s' and '%s' may not be combined",
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
259 method, prop);
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
260 else
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
261 error ("normalize: unknown method '%s'", prop);
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
262 endif
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
263 endif
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
264 endif
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
265
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
266 ## Determine method and whether there's an appropriate option specified
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
267 switch (prop)
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
268 case "zscore"
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
269 method = "zscore";
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
270 if (vararg_idx < n_varargin)
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
271 nextprop = tolower (varargin{vararg_idx+1});
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
272 if (strcmp (nextprop, "std") || strcmp (nextprop, "robust"))
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
273 methodoption = nextprop;
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
274 vararg_idx++;
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
275 endif
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
276 endif
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
277 if (isempty (methodoption))
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
278 methodoption = "std";
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
279 endif
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
280
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
281 case "norm"
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
282 method = "norm";
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
283 if (vararg_idx < n_varargin && isnumeric (varargin{vararg_idx+1}))
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
284 nextprop = varargin{vararg_idx+1};
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
285 if (isscalar (nextprop) && (nextprop > 0))
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
286 methodoption = nextprop;
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
287 vararg_idx++;
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
288 else
31353
fa3ae8fd8449 normalize.m: correct error messages split across lines (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 31352
diff changeset
289 error (["normalize: 'norm' option must be a positive ", ...
fa3ae8fd8449 normalize.m: correct error messages split across lines (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 31352
diff changeset
290 "scalar or Inf"]);
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
291 endif
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
292 endif
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
293 if (isempty (methodoption))
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
294 methodoption = 2;
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
295 endif
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
296
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
297 case "range"
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
298 method = "range";
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
299 if (vararg_idx < n_varargin && isnumeric (varargin{vararg_idx+1}))
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
300 nextprop = varargin{vararg_idx+1};
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
301 if (any (size (nextprop) != [1 2]))
31353
fa3ae8fd8449 normalize.m: correct error messages split across lines (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 31352
diff changeset
302 error (["normalize: 'range' must be specified as a ", ...
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
303 "2-element row vector [a, b]"]);
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
304 endif
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
305 methodoption = nextprop;
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
306 vararg_idx++;
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
307 endif
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
308 if (isempty (methodoption))
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
309 methodoption = [0, 1];
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
310 endif
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
311
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
312 case "scale"
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
313 method = "scale";
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
314 if (vararg_idx < n_varargin)
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
315 nextprop = tolower (varargin{vararg_idx+1});
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
316 if (isnumeric (nextprop))
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
317 if (! isscalar (nextprop))
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
318 error ("normalize: scale value must be a scalar");
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
319 else
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
320 methodoption = nextprop;
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
321 vararg_idx++;
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
322 endif
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
323 elseif (any (strcmp (nextprop, {"std", "mad", "first", "iqr"})))
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
324 methodoption = nextprop;
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
325 vararg_idx++;
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
326 endif
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
327 endif
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
328
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
329 if (isempty (methodoption))
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
330 methodoption = 'std';
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
331 endif
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
332
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
333 case "center"
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
334 method = "center";
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
335 if (vararg_idx < n_varargin)
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
336 nextprop = tolower (varargin{vararg_idx+1});
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
337 if (isscalar (nextprop)
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
338 || any (strcmp (nextprop, {"mean", "median"})))
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
339 methodoption = nextprop;
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
340 vararg_idx++;
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
341 elseif (isnumeric (nextprop))
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
342 error ("normalize: center shift must be a scalar value");
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
343 endif
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
344 endif
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
345 if (isempty (methodoption))
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
346 methodoption = 'mean';
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
347 endif
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
348
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
349 case "medianiqr"
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
350 method = "medianiqr";
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
352 otherwise
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
353 error ("normalize: unknown method '%s'", prop);
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
354
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
355 endswitch
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
356 endif
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
357
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
358 vararg_idx++;
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
359 endwhile
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
360
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
361 if (scale_and_center_flag)
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
362 method = "scaleandcenter";
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
363 endif
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
364
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
365 if (isempty (method))
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
366 method = 'zscore';
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
367 methodoption = 'std';
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
368 endif
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
369
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
370 if (isempty (dim))
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
371 ## Operate on first non-singleton dimension.
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
372 (dim = find (size (x) > 1, 1)) || (dim = 1);
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
373 endif
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
374
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
375 ## Perform normalization based on specified methods
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
376
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
377 ## FIXME: DataTables option not handled below. Fix after Table Class
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
378 ## has been implemented.
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
379
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
380 ## Default center/scale factors:
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
381 c = 0;
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
382 s = 1;
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
383
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
384 switch (method)
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
385 case "zscore"
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
386 switch (methodoption)
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
387 case "std"
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
388 [s, c] = std (x, [], dim);
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
389 case "robust"
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
390 ## center/median to zero and MAD = 1
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
391 c = median (x, dim);
31379
84fa33608593 normalize.m: Use bsxfun rather than broadcasting (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 31366
diff changeset
392 ## FIXME: Use bsxfun, rather than broadcasting, until broadcasting
84fa33608593 normalize.m: Use bsxfun rather than broadcasting (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 31366
diff changeset
393 ## supports diagonal and sparse matrices (Bugs #41441, #35787).
84fa33608593 normalize.m: Use bsxfun rather than broadcasting (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 31366
diff changeset
394 s = median (abs (bsxfun (@minus, x , c)), dim);
84fa33608593 normalize.m: Use bsxfun rather than broadcasting (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 31366
diff changeset
395 ## s = median (abs (x - c), dim); # Automatic broadcasting
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
396 endswitch
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
397
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
398 case "norm"
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
399 switch (methodoption)
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
400 case 1
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
401 s = sum (abs (x), dim);
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
402 case Inf
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
403 s = max (abs (x), [], dim);
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
404 otherwise
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
405 s = sum (abs (x) .^ methodoption, dim) .^ (1/methodoption);
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
406 endswitch
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
407
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
408 case "range"
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
409 ## if any range element = 0, avoid divide by zero by replacing that
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
410 ## range element with 1. output will be zero+min due to x-min(x)=0.
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
411 x_range = range (x, dim);
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
412 x_range(x_range == 0) = 1;
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
413 z_range = methodoption(2) - methodoption(1);
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
414 s = x_range ./ z_range;
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
415 c = min (x, [], dim) - (methodoption(1) .* s);
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
416
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
417 case "scale"
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
418 s = process_scale_option (x, dim, methodoption);
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
419
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
420 case "center"
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
421 c = process_center_option (x, dim, methodoption);
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
422
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
423 case "scaleandcenter"
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
424 ## repeats scale and center using appropriate order and info
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
425
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
426 switch (stored_method)
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
427 case "scale"
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
428 ## stored info is scale, latest info is center
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
429 center_option = methodoption;
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
430 scale_option = stored_methodoption;
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
431
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
432 case "center"
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
433 ## stored info is center, latest info is scale
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
434 center_option = stored_methodoption;
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
435 scale_option = methodoption;
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
436 endswitch
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
437
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
438 s = process_scale_option (x, dim, scale_option);
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
439 c = process_center_option (x, dim, center_option);
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
440
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
441 case "medianiqr"
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
442 c = median (x, dim);
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
443 s = iqr (x, dim);
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
444
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
445 endswitch
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
446
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
447 endif
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
448
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
449 ## Divide by scale factor. If scale = 0, divide by zero = Inf, which is OK.
31379
84fa33608593 normalize.m: Use bsxfun rather than broadcasting (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 31366
diff changeset
450
84fa33608593 normalize.m: Use bsxfun rather than broadcasting (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 31366
diff changeset
451 ## FIXME: Use bsxfun, rather than broadcasting, until broadcasting
84fa33608593 normalize.m: Use bsxfun rather than broadcasting (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 31366
diff changeset
452 ## supports diagonal and sparse matrices (Bugs #41441, #35787).
84fa33608593 normalize.m: Use bsxfun rather than broadcasting (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 31366
diff changeset
453 z = bsxfun (@rdivide, bsxfun (@minus, x , c), s);
84fa33608593 normalize.m: Use bsxfun rather than broadcasting (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 31366
diff changeset
454 ## z = (x - c) ./ s; # Automatic broadcasting
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
455
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
456 endfunction
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
457
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
458 function c = process_center_option (x, dim, center_option)
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
459
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
460 if (isnumeric (center_option))
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
461 c = center_option;
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
462 else
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
463 switch (center_option)
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
464 case "mean"
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
465 c = mean (x, dim);
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
466 case "median"
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
467 c = median (x, dim);
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
468 endswitch
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
469 endif
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
470
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
471 endfunction
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
472
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
473 function s = process_scale_option (x, dim, scale_option)
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
474
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
475 warning ("off", "Octave:divide-by-zero", "local");
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
476
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
477 if (isnumeric (scale_option))
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
478 s = scale_option;
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
479 else
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
480 switch (scale_option)
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
481 case "std"
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
482 s = std (x, [], dim);
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
483 case "mad"
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
484 s = mad (x, 1, dim);
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
485 case "first"
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
486 dim_vector = repmat ({':'}, ndims(x), 1);
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
487 dim_vector{dim} = 1;
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
488 s = x(dim_vector{:});
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
489 case "iqr"
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
490 s = iqr (x, dim);
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
491 endswitch
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
492 endif
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
493
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
494 endfunction
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
495
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
496
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
497 ## no method specified, using zscore & std
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
498 %!assert (normalize ([1,2,3]), [-1,0,1])
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
499 %!assert (normalize ([1,2,3], 2), [-1,0,1])
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
500 %!assert (normalize (single ([1,2,3])), single ([-1,0,1]))
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
501 %!assert (normalize ([2,0,-2;0,2,0;-2,-2,2]), [1,0,-1;0,1,0;-1,-1,1])
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
502 %!assert (normalize (magic (3)), [[3;-2;-1]/sqrt(7),[-1;0;1],[1;2;-3]/sqrt(7)])
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
503 %!assert (normalize (magic (3), 2), [[3 -4 1]/sqrt(13);[-1 0 1];[-1 4 -3]/sqrt(13)])
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
504
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
505 ## Method: zscore, [std, robust]
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
506 %!assert (normalize ([1,2,3],"zscore","std"), [-1,0,1])
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
507 %!assert (normalize ([2,0,-2;0,2,0;-2,-2,2],"zscore","std"), [1,0,-1;0,1,0;-1,-1,1])
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
508 %!assert (normalize (magic (3),"zscore","std"), [[3;-2;-1]/sqrt(7),[-1;0;1],[1;2;-3]/sqrt(7)])
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
509 %!assert (normalize ([1,2,3],"zscore","robust"), [-1,0,1])
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
510 %!assert (normalize ([2,0,-2;0,2,0;-2,-2,2],"zscore","robust"), [1,0,-1;0,1,0;-1,-1,1])
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
511 %!assert (normalize (magic (3),"zscore","robust"), [4 -1 0; -1 0 1; 0 1 -4])
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
512
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
513 ## Method: norm [1, 2, inf]
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
514 %!assert (normalize ([1,2,3],"norm",1), [1/6 1/3 1/2])
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
515 %!assert (normalize ([2,0,-2;0,2,0;-2,-2,2],"norm",1), [1,0,-1;0,1,0;-1,-1,1]/2)
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
516 %!assert (normalize (magic (3),"norm",1), magic(3)/15)
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
517 %!assert (normalize ([1,2,3],"norm",2), [1 2 3]./3.741657386773941, eps)
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
518 %!assert (normalize ([2,0,-2;0,2,0;-2,-2,2],"norm",2), [1,0,-1;0,1,0;-1,-1,1]*(sqrt(2)/2), eps)
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
519 %!assert (normalize ([1,2,3],"norm",Inf), [1/3 2/3 1])
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
520 %!assert (normalize ([2,0,-2;0,2,0;-2,-2,2],"norm",Inf), [1,0,-1;0,1,0;-1,-1,1])
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
521 %!assert (normalize (magic (3),"norm",Inf), [[8;3;4]/8,[1;5;9]/9,[6;7;2]/7])
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
522
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
523 ## Method: range
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
524 %!assert (normalize ([1,2,3],"range"), [0 0.5 1])
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
525 %!assert (normalize ([2,0,-2;0,2,0;-2,-2,2],"range",[0 1]), [1,0.5,0;0.5,1,0.5;0,0,1])
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
526 %!assert (normalize (magic (3),"range",[-1 1]), [1 -1 0.6; -1 0 1; -0.6 1 -1], eps)
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
527
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
528 ## Method: scale [mad first iqr number]
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
529 %!assert (normalize ([1,2,3],"scale"), [1 2 3])
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
530 %!assert (normalize ([2,0,-2;0,2,0;-2,-2,2],"scale","std"), [1 0 -1; 0 1 0; -1 -1 1])
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
531 %!assert (normalize (magic (3),"scale",2), (magic(3)/2))
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
532
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
533 %!assert (normalize ([1,2,3],"scale", "mad"), [1 2 3])
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
534 %!assert (normalize ([2,0,-2;0,2,0;-2,-2,2],"scale","mad"), [1 0 -1; 0 1 0; -1 -1 1])
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
535 %!assert (normalize (magic (3),"scale","mad"), [8 0.25 6; 3 1.25 7; 4 2.25 2])
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
536
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
537 %!assert (normalize ([1,2,3],"scale", "first"), [1 2 3])
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
538 %!assert (normalize ([2,0,-2;0,2,0;-2,-2,2],"scale","first"), [1 NaN 1; 0 Inf 0; -1 -Inf -1])
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
539 %!assert (normalize (magic (3),"scale","first"), [1 1 1; 3/8 5 7/6; 0.5 9 1/3])
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
540 %!assert (normalize (magic (3),2,"scale","first"), [1 1/8 3/4;1 5/3 7/3;1 9/4 0.5])
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
541 %!test
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
542 %! x = reshape (magic (4),2,2,2,2);
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
543 %! y3 = cat (4, cat (3,ones(2),[1/8 7/9;11/5 7/2]), cat (3,ones(2),[13/3 2; 4/5 1/15]));
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
544 %! y4 = cat (4, ones (2,2,2), cat (3,[3/16 2/3; 2 15/4],[6.5 12/7; 8/11 1/14] ));
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
545 %! assert (normalize (x, 3, "scale", "first"), y3);
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
546 %! assert (normalize (x, 4, "scale", "first"), y4);
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
547
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
548 %!assert (normalize ([1,2,3], "scale", "iqr"), [1 2 3]*2/3)
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
549 %!assert (normalize ([1,2,3]', "scale", "iqr"), ([1 2 3]')*2/3)
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
550 %!assert (normalize ([2,0,-2;0,2,0;-2,-2,2],"scale","iqr"), [1 0 -1; 0 1 0; -1 -1 1]* 2/3, eps)
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
551 %!assert (normalize (magic (3),"scale","iqr"), [[8;3;4]/3.75,[1;5;9]/6,[6;7;2]/3.75],eps)
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
552
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
553 ## Method: center [mean median number]
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
554 %!assert (normalize ([1,2,3], "center"), [-1 0 1])
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
555 %!assert (normalize ([1,2,3], 1, "center"), [0 0 0])
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
556 %!assert (normalize ([1,2,3], "center", 10), [-9 -8 -7])
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
557 %!assert (normalize ([1 2 3 10], "center", "mean"), [-3 -2 -1 6])
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
558 %!assert (normalize ([1 2 3 10], "center", "median"), [-1.5 -0.5 0.5 7.5])
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
559
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
560 ## Method: medianiqr
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
561 %!assert (normalize ([1,2,3], "medianiqr"), [-1 0 1]*2/3)
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
562 %!assert (normalize ([1,2,3]', "medianiqr"), ([-1 0 1]')*2/3)
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
563 %!assert (normalize ([2,0,-2;0,2,0;-2,-2,2], "medianiqr"), [1 0 -1; 0 1 0; -1 -1 1]*2/3)
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
564 %!assert (normalize (magic (3), "medianiqr"), [8/5 -1 0; -2/5 0 2/5; 0 1 -8/5]*2/3)
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
565
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
566 ## Test NaN and Inf
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
567 %!assert (normalize ([1 2 Inf], 2), [NaN, NaN, NaN])
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
568 %!assert (normalize ([1 2 3], 1), [NaN, NaN, NaN])
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
569 %!assert (normalize ([1 2 3], 3), [NaN, NaN, NaN])
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
570 %!assert (normalize (ones (3,2,2,2)), NaN (3,2,2,2))
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
571 %!assert (normalize (Inf), NaN)
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
572 %!assert (normalize (NaN), NaN)
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
573 %!assert (normalize ([Inf, NaN]), [NaN, NaN])
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
574 %!assert (normalize ([Inf, NaN]'), [NaN, NaN]')
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
575 %!assert (normalize ([Inf, Inf], 1), [NaN, NaN])
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
576 %!assert (normalize ([Inf, Inf], 2), [NaN, NaN])
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
577 %!assert (normalize ([Inf, Inf]', 1), [NaN, NaN]')
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
578 %!assert (normalize ([Inf, Inf]', 2), [NaN, NaN]')
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
579 %!assert (normalize ([1 2 NaN; NaN 3 4], 1), [NaN -1 NaN; NaN 1 NaN]*sqrt(2)/2, eps)
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
580
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
581 ## Two input methods, must be scale and center
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
582 %!assert (normalize (magic(3), "scale", "center"), normalize (magic(3), "zscore"), eps)
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
583 %!assert (normalize (magic(3), "center", "scale"), normalize (magic(3), "zscore"), eps)
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
584
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
585 ## Test additional outputs
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
586 %!test
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
587 %! [z, c, s] = normalize ([1, 2, 3], 2);
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
588 %! assert ({z, c, s}, {[-1 0 1], [2], [1]});
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
589 %! [z, c, s] = normalize (magic (3), "zscore", "std");
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
590 %! assert ({z, c, s}, {[[3;-2;-1]/sqrt(7),[-1;0;1],[1;2;-3]/sqrt(7)], [5 5 5], [sqrt(7) 4 sqrt(7)]});
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
591 %! [z, c, s] = normalize (magic (3), "zscore", "robust");
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
592 %! assert ({z, c, s}, {[4 -1 0; -1 0 1; 0 1 -4], [4 5 6], [1 4 1]});
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
593 %! [z, c, s] = normalize (magic (3), "norm", 1);
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
594 %! assert ({z, c, s}, {magic(3)/15 , 0, [15 15 15]});
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
595 %! [z, c, s] = normalize ([2,0,-2;0,2,0;-2,-2,2],"norm",2);
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
596 %! assert ({z, c, s}, {[1,0,-1;0,1,0;-1,-1,1]*(sqrt(2)/2), 0, [1 1 1]*2*sqrt(2)}, eps)
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
597 %! [z, c, s] = normalize ([1 2 3], "norm", Inf);
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
598 %! assert ({z, c, s}, {[1 2 3]/3, 0, 3}, eps);
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
599 %! [z, c, s] = normalize (magic (3),"range",[-1 1]);
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
600 %! assert ({z, c, s}, {[1 -1 0.6; -1 0 1; -0.6 1 -1], [5.5 5 4.5], [2.5 4 2.5]}, eps)
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
601 %! [z, c, s] = normalize (magic (3),"scale","mad");
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
602 %! assert ({z, c, s}, {[8 0.25 6; 3 1.25 7; 4 2.25 2], 0, [1 4 1]});
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
603 %! [z, c, s] = normalize (magic (3),"scale","first");
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
604 %! assert ({z, c, s}, {[1 1 1; 3/8 5 7/6; 0.5 9 1/3],0, [8 1 6]}, eps);
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
605 %! [z, c, s] = normalize ([1,2,3]', "scale", "iqr");
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
606 %! assert ({z, c, s}, {([1 2 3]')*2/3, 0, 1.5});
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
607 %! [z, c, s] = normalize ([1,2,3], "center", 10);
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
608 %! assert ({z, c, s}, {[-9 -8 -7], 10, 1});
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
609 %! [z, c, s] = normalize ([1 2 3 10], "center", "mean");
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
610 %! assert ({z, c, s}, {[-3 -2 -1 6], 4, 1})
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
611 %! [z, c, s] = normalize ([1 2 3 10], "center", "median");
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
612 %! assert ({z, c, s}, {[-1.5 -0.5 0.5 7.5], 2.5, 1});
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
613 %! [z, c, s] = normalize (magic (3), "medianiqr");
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
614 %! assert ({z, c, s}, {[8/5 -1 0; -2/5 0 2/5; 0 1 -8/5]*2/3, [4 5 6], [3.75 6 3.75]}, eps)
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
615 %! [z, c, s] = normalize ([1 2 Inf], 2);
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
616 %! assert ({z, c, s}, {[NaN, NaN, NaN], Inf, NaN});
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
617 %! [z, c, s] = normalize (Inf);
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
618 %! assert ({z, c, s}, {NaN, Inf, NaN});
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
619
31379
84fa33608593 normalize.m: Use bsxfun rather than broadcasting (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 31366
diff changeset
620 ## Test sparse and diagonal inputs
84fa33608593 normalize.m: Use bsxfun rather than broadcasting (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 31366
diff changeset
621 %!test
84fa33608593 normalize.m: Use bsxfun rather than broadcasting (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 31366
diff changeset
622 %! [z, c, s] = normalize (eye (2));
84fa33608593 normalize.m: Use bsxfun rather than broadcasting (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 31366
diff changeset
623 %! assert (z, (sqrt(2)/2)*[1, -1; -1, 1], eps);
84fa33608593 normalize.m: Use bsxfun rather than broadcasting (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 31366
diff changeset
624 %! assert (c, [0.5, 0.5], eps);
84fa33608593 normalize.m: Use bsxfun rather than broadcasting (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 31366
diff changeset
625 %! assert (s, (sqrt(2)/2)*[1, 1], eps);
84fa33608593 normalize.m: Use bsxfun rather than broadcasting (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 31366
diff changeset
626 %!test
84fa33608593 normalize.m: Use bsxfun rather than broadcasting (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 31366
diff changeset
627 %! [z, c, s] = normalize (sparse (eye (2)));
84fa33608593 normalize.m: Use bsxfun rather than broadcasting (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 31366
diff changeset
628 %! assert (full (z), (sqrt(2)/2)*[1, -1; -1, 1], eps);
84fa33608593 normalize.m: Use bsxfun rather than broadcasting (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 31366
diff changeset
629 %! assert (full (c), [0.5, 0.5], eps);
84fa33608593 normalize.m: Use bsxfun rather than broadcasting (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 31366
diff changeset
630 %! assert (full (s), (sqrt(2)/2)*[1, 1], eps);
84fa33608593 normalize.m: Use bsxfun rather than broadcasting (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 31366
diff changeset
631 %!test
84fa33608593 normalize.m: Use bsxfun rather than broadcasting (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 31366
diff changeset
632 %! [z, c, s] = normalize (sparse (magic (3)), "zscore", "robust");
84fa33608593 normalize.m: Use bsxfun rather than broadcasting (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 31366
diff changeset
633 %! assert (full (z), [4 -1 0; -1 0 1; 0 1 -4], eps);
84fa33608593 normalize.m: Use bsxfun rather than broadcasting (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 31366
diff changeset
634 %! assert (full (c), [4, 5, 6], eps);
84fa33608593 normalize.m: Use bsxfun rather than broadcasting (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 31366
diff changeset
635 %! assert (full (s), [1, 4, 1], eps);
84fa33608593 normalize.m: Use bsxfun rather than broadcasting (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 31366
diff changeset
636 %!test <55765>
84fa33608593 normalize.m: Use bsxfun rather than broadcasting (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 31366
diff changeset
637 %! [z, c, s] = normalize (sparse (eye(2)));
84fa33608593 normalize.m: Use bsxfun rather than broadcasting (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 31366
diff changeset
638 %! assert (issparse (z));
84fa33608593 normalize.m: Use bsxfun rather than broadcasting (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 31366
diff changeset
639 %! assert (issparse (c));
84fa33608593 normalize.m: Use bsxfun rather than broadcasting (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 31366
diff changeset
640 %! assert (issparse (s));
84fa33608593 normalize.m: Use bsxfun rather than broadcasting (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 31366
diff changeset
641 %!test <55765>
84fa33608593 normalize.m: Use bsxfun rather than broadcasting (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 31366
diff changeset
642 %! [z, c, s] = normalize (sparse (magic (3)), "zscore", "robust");
84fa33608593 normalize.m: Use bsxfun rather than broadcasting (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 31366
diff changeset
643 %! assert (issparse (z));
84fa33608593 normalize.m: Use bsxfun rather than broadcasting (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 31366
diff changeset
644 %! assert (issparse (c));
84fa33608593 normalize.m: Use bsxfun rather than broadcasting (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 31366
diff changeset
645 %! assert (issparse (s));
84fa33608593 normalize.m: Use bsxfun rather than broadcasting (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 31366
diff changeset
646
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
647 ## Matlab ignores NaNs, operating as if the vector had one less element, then
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
648 ## returns the result retaining the NaN in the solution.
31352
014648ce3267 normalize.m: Add xtest bug number and cleanup line-lengths (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 31351
diff changeset
649 %!assert <50571> (normalize ([1 2 NaN], 2), [-1, 1, NaN]*sqrt(2)/2)
014648ce3267 normalize.m: Add xtest bug number and cleanup line-lengths (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 31351
diff changeset
650 %!assert <50571> (normalize ([1 2 NaN; 1 2 3], 2), [[-1 1 NaN]*sqrt(2)/2; -1 0 1], eps)
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
651
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
652 ## Test input validation
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
653 %!error <Invalid call> normalize ()
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
654 %!error <Invalid call> normalize (1, 2, 3)
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
655 %!error <X must be a numeric> normalize (['A'; 'B'])
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
656 %!error <DIM must be an integer> normalize (1, ones (2,2))
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
657 %!error <DIM must be an integer> normalize (1, 1.5)
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
658 %!error <DIM must be .* a valid dimension> normalize (1, 0)
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
659 %!error <more than two methods specified> normalize ([1 2 3], "scale", "center", "norm")
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
660 %!error <methods .* may not be combined> normalize ([1 2 3], "norm", "zscore")
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
661 %!error <unknown method 'foo'> normalize ([1 2 3], "norm", "foo")
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
662 %
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
663 %!error <'norm' option must be a positive scalar or Inf> normalize ([1 2 3], "norm", [1 2])
31353
fa3ae8fd8449 normalize.m: correct error messages split across lines (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 31352
diff changeset
664 %!error <'norm' option must be a positive scalar or Inf> normalize ([1 2 3], "norm", -1)
31351
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
665 %!error <'range' must be specified as> normalize ([1 2 3], "range", [1 2]')
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
666 %!error <'range' must be specified as> normalize ([1 2 3], "range", [1 2 3])
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
667 %!error <'range' must be specified as> normalize ([1 2 3], "range", 1)
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
668 %!error <scale value must be a scalar> normalize ([1 2 3], "scale", [1 2 3])
ecfb41743fd2 Implement new function 'normalize' (bug #55765)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
diff changeset
669 %!error <center shift must be a scalar value> normalize ([1 2 3], "center", [1 2])
31358
1193e869de98 normalize.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31356
diff changeset
670 %!error <unknown method 'foo'> normalize ([1 2 3], "foo")