# HG changeset patch
# User Nicholas R. Jankowski <jankowskin@asme.org>
# Date 1471290621 14400
#      Mon Aug 15 15:50:21 2016 -0400
# Node ID b39e1253b8d6b734a61a90ebeb66c544ec4bc593
# Parent  9e8ff835d00c0ca54c50ce16334f5f260e57e9e5
Edited mean.m so that emtpy input produces NaN with appropriate size, preserving single or double. Also changed return variable name to retval to match other basic statistics functions (bug #48690)

diff -r 9e8ff835d00c -r b39e1253b8d6 scripts/statistics/base/mean.m
--- a/scripts/statistics/base/mean.m	Mon Aug 15 15:44:24 2016 -0400
+++ b/scripts/statistics/base/mean.m	Mon Aug 15 15:50:21 2016 -0400
@@ -62,7 +62,7 @@
 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
 ## Description: Compute arithmetic, geometric, and harmonic mean
 
-function y = mean (x, opt1, opt2)
+function retval = mean (x, opt1, opt2)
 
   if (nargin < 1 || nargin > 3)
     print_usage ();
@@ -111,17 +111,34 @@
   endif
 
   n = size (x, dim);
+  
+  if (isempty (x))
+     %% codepath for Matlab Compatibility. empty x produces NaN output, but 
+    %% for ndim > 2, output depends on size of x.  tests include for
+    %% compatibility with Matlab 2016a, includes all empty arrays up to dim4,
+    %% sizes of 0 1 or 2 in each dim.
+    %% TODO:  update this if later Matlab versions show more output consistency
+    if ((nd == 2) && (max (sz) < 2))
+      retval = NaN;
+    else
+      sz (find ((sz ~= 1), 1)) = 1;
+      retval = NaN (sz);
+    endif
 
-  if (strcmp (opt, "a"))
-    y = sum (x, dim) / n;
+    if (isa (x, "single"))
+      retval = single (retval);  
+    endif
+
+  elseif (strcmp (opt, "a"))
+    retval = sum (x, dim) / n;
   elseif (strcmp (opt, "g"))
     if (all (x(:) >= 0))
-      y = exp (sum (log (x), dim) ./ n);
+      retval = exp (sum (log (x), dim) ./ n);
     else
       error ("mean: X must not contain any negative values");
     endif
   elseif (strcmp (opt, "h"))
-    y = n ./ sum (1 ./ x, dim);
+    retval = n ./ sum (1 ./ x, dim);
   else
     error ("mean: option '%s' not recognized", opt);
   endif
@@ -148,6 +165,83 @@
 %!assert (mean (single ([1 0 1 1])), single (0.75))
 %!assert (mean ([1 2], 3), [1 2])
 
+##tests for empty input matlab compatibility (v2016a)
+%!assert (mean ([]), NaN)
+%!assert (mean (single([])), single(NaN))
+
+%!assert (mean (ones (0,0,0,0)), NaN (1,0,0,0))
+%!assert (mean (ones (0,0,0,1)), NaN (1,0,0,1))
+%!assert (mean (ones (0,0,0,2)), NaN (1,0,0,2))
+%!assert (mean (ones (0,0,1,0)), NaN (1,0,1,0))
+%!assert (mean (ones (0,0,1,1)), NaN (1,1,1,1))
+%!assert (mean (ones (0,0,1,2)), NaN (1,0,1,2))
+%!assert (mean (ones (0,0,2,0)), NaN (1,0,2,0))
+%!assert (mean (ones (0,0,2,1)), NaN (1,0,2,1))
+%!assert (mean (ones (0,0,2,2)), NaN (1,0,2,2))
+%!assert (mean (ones (0,1,0,0)), NaN (1,1,0,0))
+%!assert (mean (ones (0,1,0,1)), NaN (1,1,0,1))
+%!assert (mean (ones (0,1,0,2)), NaN (1,1,0,2))
+%!assert (mean (ones (0,1,1,0)), NaN (1,1,1,0))
+%!assert (mean (ones (0,1,1,1)), NaN (1,1,1,1))
+%!assert (mean (ones (0,1,1,2)), NaN (1,1,1,2))
+%!assert (mean (ones (0,1,2,0)), NaN (1,1,2,0))
+%!assert (mean (ones (0,1,2,1)), NaN (1,1,2,1))
+%!assert (mean (ones (0,1,2,2)), NaN (1,1,2,2))
+%!assert (mean (ones (0,2,0,0)), NaN (1,2,0,0))
+%!assert (mean (ones (0,2,0,1)), NaN (1,2,0,1))
+%!assert (mean (ones (0,2,0,2)), NaN (1,2,0,2))
+%!assert (mean (ones (0,2,1,0)), NaN (1,2,1,0))
+%!assert (mean (ones (0,2,1,1)), NaN (1,2,1,1))
+%!assert (mean (ones (0,2,1,2)), NaN (1,2,1,2))
+%!assert (mean (ones (0,2,2,0)), NaN (1,2,2,0))
+%!assert (mean (ones (0,2,2,1)), NaN (1,2,2,1))
+%!assert (mean (ones (0,2,2,2)), NaN (1,2,2,2))
+%!assert (mean (ones (1,0,0,0)), NaN (1,1,0,0))
+%!assert (mean (ones (1,0,0,1)), NaN (1,1,0,1))
+%!assert (mean (ones (1,0,0,2)), NaN (1,1,0,2))
+%!assert (mean (ones (1,0,1,0)), NaN (1,1,1,0))
+%!assert (mean (ones (1,0,1,1)), NaN (1,1,1,1))
+%!assert (mean (ones (1,0,1,2)), NaN (1,1,1,2))
+%!assert (mean (ones (1,0,2,0)), NaN (1,1,2,0))
+%!assert (mean (ones (1,0,2,1)), NaN (1,1,2,1))
+%!assert (mean (ones (1,0,2,2)), NaN (1,1,2,2))
+%!assert (mean (ones (1,1,0,0)), NaN (1,1,1,0))
+%!assert (mean (ones (1,1,0,1)), NaN (1,1,1,1))
+%!assert (mean (ones (1,1,0,2)), NaN (1,1,1,2))
+%!assert (mean (ones (1,1,1,0)), NaN (1,1,1,1))
+%!assert (mean (ones (1,1,2,0)), NaN (1,1,1,0))
+%!assert (mean (ones (1,2,0,0)), NaN (1,1,0,0))
+%!assert (mean (ones (1,2,0,1)), NaN (1,1,0,1))
+%!assert (mean (ones (1,2,0,2)), NaN (1,1,0,2))
+%!assert (mean (ones (1,2,1,0)), NaN (1,1,1,0))
+%!assert (mean (ones (1,2,2,0)), NaN (1,1,2,0))
+%!assert (mean (ones (2,0,0,0)), NaN (1,0,0,0))
+%!assert (mean (ones (2,0,0,1)), NaN (1,0,0,1))
+%!assert (mean (ones (2,0,0,2)), NaN (1,0,0,2))
+%!assert (mean (ones (2,0,1,0)), NaN (1,0,1,0))
+%!assert (mean (ones (2,0,1,1)), NaN (1,0,1,1))
+%!assert (mean (ones (2,0,1,2)), NaN (1,0,1,2))
+%!assert (mean (ones (2,0,2,0)), NaN (1,0,2,0))
+%!assert (mean (ones (2,0,2,1)), NaN (1,0,2,1))
+%!assert (mean (ones (2,0,2,2)), NaN (1,0,2,2))
+%!assert (mean (ones (2,1,0,0)), NaN (1,1,0,0))
+%!assert (mean (ones (2,1,0,1)), NaN (1,1,0,1))
+%!assert (mean (ones (2,1,0,2)), NaN (1,1,0,2))
+%!assert (mean (ones (2,1,1,0)), NaN (1,1,1,0))
+%!assert (mean (ones (2,1,2,0)), NaN (1,1,2,0))
+%!assert (mean (ones (2,2,0,0)), NaN (1,2,0,0))
+%!assert (mean (ones (2,2,0,1)), NaN (1,2,0,1))
+%!assert (mean (ones (2,2,0,2)), NaN (1,2,0,2))
+%!assert (mean (ones (2,2,1,0)), NaN (1,2,1,0))
+%!assert (mean (ones (2,2,2,0)), NaN (1,2,2,0))
+%!assert (mean (ones(1,1,0,0,0)), NaN (1,1,1,0,0))
+%!assert (mean (ones(1,1,1,1,0)), NaN (1,1,1,1,1))
+%!assert (mean (ones(2,1,1,1,0)), NaN (1,1,1,1,0))
+%!assert (mean (ones(1,2,1,1,0)), NaN (1,1,1,1,0))
+
+%!assert (mean (ones (1,3,0,2)), NaN (1,1,0,2)) 
+%!assert (mean (single (ones (1,3,0,2))), single (NaN (1,1,0,2)))
+
 ## Test input validation
 %!error mean ()
 %!error mean (1, 2, 3, 4)
