changeset 25817:dc70b480b9c1 stable

zscore.m: Allow an empty OPT argument to mean default (bug #54531). * zscore.m: Set default for second argument opt to 0 in function prototype. Check whether opt is empty and assign value of 0 if it is. Add BIST test for bug #54531. Add expected error for each input validation BIST test.
author Rik <rik@octave.org>
date Sun, 19 Aug 2018 19:01:31 -0700
parents a8a72d1c21dd
children 182a8207a313
files scripts/statistics/zscore.m
diffstat 1 files changed, 11 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/statistics/zscore.m	Sat Aug 18 12:43:43 2018 -0400
+++ b/scripts/statistics/zscore.m	Sun Aug 19 19:01:31 2018 -0700
@@ -43,7 +43,7 @@
 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
 ## Description: Subtract mean and divide by standard deviation
 
-function [z, mu, sigma] = zscore (x, opt, dim)
+function [z, mu, sigma] = zscore (x, opt = 0, dim)
 
   if (nargin < 1 || nargin > 3 )
     print_usage ();
@@ -53,12 +53,10 @@
     error ("zscore: X must be a numeric vector or matrix");
   endif
 
-  if (nargin < 2)
+  if (isempty (opt))
     opt = 0;
-  else
-    if (opt != 0 && opt != 1 || ! isscalar (opt))
-      error ("zscore: OPT must be empty, 0, or 1");
-    endif
+  elseif (! isscalar (opt) || (opt != 0 && opt != 1))
+    error ("zscore: normalization OPT must be empty, 0, or 1");
   endif
 
   nd = ndims (x);
@@ -97,12 +95,14 @@
 %!assert (zscore (int8 ([1,2,3])), [-1,0,1])
 %!assert (zscore (ones (3,2,2,2)), zeros (3,2,2,2))
 %!assert (zscore ([2,0,-2;0,2,0;-2,-2,2]), [1,0,-1;0,1,0;-1,-1,1])
+%!assert <*54531> (zscore ([1,2,3], [], 2), [-1,0,1])
 
 ## Test input validation
 %!error zscore ()
 %!error zscore (1, 2, 3)
-%!error zscore (['A'; 'B'])
-%!error zscore (1, ones (2,2))
-%!error zscore (1, 1.5)
-%!error zscore (1, 1, 0)
-%!error zscore (1, 3)
+%!error <X must be a numeric> zscore (['A'; 'B'])
+%!error <OPT must be empty, 0, or 1> zscore (1, ones (2,2))
+%!error <OPT must be empty, 0, or 1> zscore (1, 1.5)
+%!error <DIM must be an integer> zscore (1, [], ones (2,2))
+%!error <DIM must be an integer> zscore (1, [], 1.5)
+%!error <DIM must be .* a valid dimension> zscore (1, [], 0)