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