# HG changeset patch
# User Nicholas R. Jankowski <jankowskin@asme.org>
# Date 1484765943 18000
#      Wed Jan 18 13:59:03 2017 -0500
# Node ID 29c69084ea5a91c1ceae21c85fbfab6cd01acc32
# Parent  7c7a61c2e0edf0ad9249e1c48cb3b9759db0d5be
Edited statistics functions to produce matlab compatible ouput on emtpy input (bug #48690)

* /scripts/statistics/base/std.m, var.m, mean.m, mode.m, median.m: empty input produces appropriate size NaN, preserving single or double, and handles all output variables where applicable, with comprehensive tests.

* note: does note include cov.m pending further discussion on proper output

diff -r 7c7a61c2e0ed -r 29c69084ea5a scripts/statistics/base/mean.m
--- a/scripts/statistics/base/mean.m	Tue Jan 17 17:42:27 2017 -0800
+++ b/scripts/statistics/base/mean.m	Wed Jan 18 13:59:03 2017 -0500
@@ -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,32 @@
   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.  
+    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
@@ -138,7 +153,7 @@
 %! assert (mean (z), [0, 10]);
 
 ## Test small numbers
-%!assert (mean (repmat (0.1,1,1000), "g"), 0.1, 20*eps)
+%!assert (mean (repmat (0.1, 1, 1000), "g"), 0.1, 20*eps)
 
 %!assert (mean (magic (3), 1), [5, 5, 5])
 %!assert (mean (magic (3), 2), [5; 5; 5])
@@ -148,12 +163,87 @@
 %!assert (mean (single ([1 0 1 1])), single (0.75))
 %!assert (mean ([1 2], 3), [1 2])
 
+##tests for empty input Matlab compatibility (bug #48690)
+%!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)
 %!error <X must be a numeric> mean ({1:5})
 %!error <OPT must be a string> mean (1, 2, 3)
-%!error <DIM must be an integer> mean (1, ones (2,2))
+%!error <DIM must be an integer> mean (1, ones (2, 2))
 %!error <DIM must be an integer> mean (1, 1.5)
 %!error <DIM must be .* a valid dimension> mean (1, 0)
 %!error <X must not contain any negative values> mean ([1 -1], "g")
diff -r 7c7a61c2e0ed -r 29c69084ea5a scripts/statistics/base/median.m
--- a/scripts/statistics/base/median.m	Tue Jan 17 17:42:27 2017 -0800
+++ b/scripts/statistics/base/median.m	Wed Jan 18 13:59:03 2017 -0500
@@ -63,10 +63,6 @@
     error ("median: X must be a numeric vector or matrix");
   endif
 
-  if (isempty (x))
-    error ("median: X cannot be an empty matrix");
-  endif
-
   nd = ndims (x);
   sz = size (x);
   if (nargin < 2)
@@ -80,14 +76,31 @@
 
   n = size (x, dim);
   k = floor ((n+1) / 2);
-  if (mod (n, 2) == 1)
+
+  if (isempty (x))
+  %% codepath for Matlab compatibility. empty x produces NaN output, but 
+  %% for ndim > 2, output depends on size of x. 
+    if ((nd == 2) && (max (sz) < 2))
+      retval = NaN;
+    else
+      sz (find ((sz ~= 1), 1)) = 1;
+      retval = NaN (sz);
+    endif
+
+    if (isa (x, "single"))
+      retval = single (retval);  
+    endif
+
+  elseif (mod (n, 2) == 1)
     retval = nth_element (x, k, dim);
+    
   else
     retval = sum (nth_element (x, k:k+1, dim), dim, "native") / 2;
     if (islogical (x))
       retval = logical (retval);
     endif
   endif
+  
   ## Inject NaNs where needed, to be consistent with Matlab.
   if (isfloat (x))
     retval(any (isnan (x), dim)) = NaN;
@@ -107,20 +120,95 @@
 %! assert (median ([x2, 2*x2]), [3.5, 7]);
 %! assert (median ([y2, 3*y2]), [4, 12]);
 
-%!assert (median (single ([1,2,3])), single (2))
-%!assert (median ([1,2,NaN;4,5,6;NaN,8,9]), [NaN, 5, NaN])
-%!assert (median ([1,2], 3), [1,2])
+%!assert (median (single ([1, 2, 3])), single (2))
+%!assert (median ([1, 2, NaN; 4, 5, 6; NaN, 8, 9]), [NaN, 5, NaN])
+%!assert (median ([1, 2], 3), [1, 2])
 
 ## Test multidimensional arrays
 %!shared a, b, x, y
 %! rand ("seed", 2);
-%! a = rand (2,3,4,5);
-%! b = rand (3,4,6,5);
+%! a = rand (2, 3, 4, 5);
+%! b = rand (3, 4, 6, 5);
 %! x = sort (a, 4);
 %! y = sort (b, 3);
 %!assert <35679> (median (a, 4), x(:, :, :, 3))
 %!assert <35679> (median (b, 3), (y(:, :, 3, :) + y(:, :, 4, :))/2)
 
+##tests for empty input Matlab compatibility (bug #48690)
+%!assert (median ([]), NaN)
+%!assert (median (single ([])), single (NaN))
+%!assert (median (ones (0, 0, 0, 0)), NaN (1, 0, 0, 0))
+%!assert (median (ones (0, 0, 0, 1)), NaN (1, 0, 0, 1))
+%!assert (median (ones (0, 0, 0, 2)), NaN (1, 0, 0, 2))
+%!assert (median (ones (0, 0, 1, 0)), NaN (1, 0, 1, 0))
+%!assert (median (ones (0, 0, 1, 1)), NaN (1, 1, 1, 1))
+%!assert (median (ones (0, 0, 1, 2)), NaN (1, 0, 1, 2))
+%!assert (median (ones (0, 0, 2, 0)), NaN (1, 0, 2, 0))
+%!assert (median (ones (0, 0, 2, 1)), NaN (1, 0, 2, 1))
+%!assert (median (ones (0, 0, 2, 2)), NaN (1, 0, 2, 2))
+%!assert (median (ones (0, 1, 0, 0)), NaN (1, 1, 0, 0))
+%!assert (median (ones (0, 1, 0, 1)), NaN (1, 1, 0, 1))
+%!assert (median (ones (0, 1, 0, 2)), NaN (1, 1, 0, 2))
+%!assert (median (ones (0, 1, 1, 0)), NaN (1, 1, 1, 0))
+%!assert (median (ones (0, 1, 1, 1)), NaN (1, 1, 1, 1))
+%!assert (median (ones (0, 1, 1, 2)), NaN (1, 1, 1, 2))
+%!assert (median (ones (0, 1, 2, 0)), NaN (1, 1, 2, 0))
+%!assert (median (ones (0, 1, 2, 1)), NaN (1, 1, 2, 1))
+%!assert (median (ones (0, 1, 2, 2)), NaN (1, 1, 2, 2))
+%!assert (median (ones (0, 2, 0, 0)), NaN (1, 2, 0, 0))
+%!assert (median (ones (0, 2, 0, 1)), NaN (1, 2, 0, 1))
+%!assert (median (ones (0, 2, 0, 2)), NaN (1, 2, 0, 2))
+%!assert (median (ones (0, 2, 1, 0)), NaN (1, 2, 1, 0))
+%!assert (median (ones (0, 2, 1, 1)), NaN (1, 2, 1, 1))
+%!assert (median (ones (0, 2, 1, 2)), NaN (1, 2, 1, 2))
+%!assert (median (ones (0, 2, 2, 0)), NaN (1, 2, 2, 0))
+%!assert (median (ones (0, 2, 2, 1)), NaN (1, 2, 2, 1))
+%!assert (median (ones (0, 2, 2, 2)), NaN (1, 2, 2, 2))
+%!assert (median (ones (1, 0, 0, 0)), NaN (1, 1, 0, 0))
+%!assert (median (ones (1, 0, 0, 1)), NaN (1, 1, 0, 1))
+%!assert (median (ones (1, 0, 0, 2)), NaN (1, 1, 0, 2))
+%!assert (median (ones (1, 0, 1, 0)), NaN (1, 1, 1, 0))
+%!assert (median (ones (1, 0, 1, 1)), NaN (1, 1, 1, 1))
+%!assert (median (ones (1, 0, 1, 2)), NaN (1, 1, 1, 2))
+%!assert (median (ones (1, 0, 2, 0)), NaN (1, 1, 2, 0))
+%!assert (median (ones (1, 0, 2, 1)), NaN (1, 1, 2, 1))
+%!assert (median (ones (1, 0, 2, 2)), NaN (1, 1, 2, 2))
+%!assert (median (ones (1, 1, 0, 0)), NaN (1, 1, 1, 0))
+%!assert (median (ones (1, 1, 0, 1)), NaN (1, 1, 1, 1))
+%!assert (median (ones (1, 1, 0, 2)), NaN (1, 1, 1, 2))
+%!assert (median (ones (1, 1, 1, 0)), NaN (1, 1, 1, 1))
+%!assert (median (ones (1, 1, 2, 0)), NaN (1, 1, 1, 0))
+%!assert (median (ones (1, 2, 0, 0)), NaN (1, 1, 0, 0))
+%!assert (median (ones (1, 2, 0, 1)), NaN (1, 1, 0, 1))
+%!assert (median (ones (1, 2, 0, 2)), NaN (1, 1, 0, 2))
+%!assert (median (ones (1, 2, 1, 0)), NaN (1, 1, 1, 0))
+%!assert (median (ones (1, 2, 2, 0)), NaN (1, 1, 2, 0))
+%!assert (median (ones (2, 0, 0, 0)), NaN (1, 0, 0, 0))
+%!assert (median (ones (2, 0, 0, 1)), NaN (1, 0, 0, 1))
+%!assert (median (ones (2, 0, 0, 2)), NaN (1, 0, 0, 2))
+%!assert (median (ones (2, 0, 1, 0)), NaN (1, 0, 1, 0))
+%!assert (median (ones (2, 0, 1, 1)), NaN (1, 0, 1, 1))
+%!assert (median (ones (2, 0, 1, 2)), NaN (1, 0, 1, 2))
+%!assert (median (ones (2, 0, 2, 0)), NaN (1, 0, 2, 0))
+%!assert (median (ones (2, 0, 2, 1)), NaN (1, 0, 2, 1))
+%!assert (median (ones (2, 0, 2, 2)), NaN (1, 0, 2, 2))
+%!assert (median (ones (2, 1, 0, 0)), NaN (1, 1, 0, 0))
+%!assert (median (ones (2, 1, 0, 1)), NaN (1, 1, 0, 1))
+%!assert (median (ones (2, 1, 0, 2)), NaN (1, 1, 0, 2))
+%!assert (median (ones (2, 1, 1, 0)), NaN (1, 1, 1, 0))
+%!assert (median (ones (2, 1, 2, 0)), NaN (1, 1, 2, 0))
+%!assert (median (ones (2, 2, 0, 0)), NaN (1, 2, 0, 0))
+%!assert (median (ones (2, 2, 0, 1)), NaN (1, 2, 0, 1))
+%!assert (median (ones (2, 2, 0, 2)), NaN (1, 2, 0, 2))
+%!assert (median (ones (2, 2, 1, 0)), NaN (1, 2, 1, 0))
+%!assert (median (ones (2, 2, 2, 0)), NaN (1, 2, 2, 0))
+%!assert (median (ones (1, 1, 0, 0, 0)), NaN (1, 1, 1, 0, 0))
+%!assert (median (ones (1, 1, 1, 1, 0)), NaN (1, 1, 1, 1, 1))
+%!assert (median (ones (2, 1, 1, 1, 0)), NaN (1, 1, 1, 1, 0))
+%!assert (median (ones (1, 2, 1, 1, 0)), NaN (1, 1, 1, 1, 0))
+%!assert (median (ones (1, 3, 0, 2)), NaN (1, 1, 0, 2)) 
+%!assert (median (single (ones (1, 3, 0, 2))), single (NaN (1, 1, 0, 2)))
+
 ## Test non-floating point types
 %!assert (median ([true, false]), true)
 %!assert (median (uint8 ([1, 3])), uint8 (2))
@@ -132,8 +220,7 @@
 %!error median ()
 %!error median (1, 2, 3)
 %!error <X must be a numeric> median ({1:5})
-%!error <X cannot be an empty matrix> median ([])
-%!error <DIM must be an integer> median (1, ones (2,2))
+%!error <DIM must be an integer> median (1, ones (2, 2))
 %!error <DIM must be an integer> median (1, 1.5)
 %!error <DIM must be .* a valid dimension> median (1, 0)
 
diff -r 7c7a61c2e0ed -r 29c69084ea5a scripts/statistics/base/mode.m
--- a/scripts/statistics/base/mode.m	Tue Jan 17 17:42:27 2017 -0800
+++ b/scripts/statistics/base/mode.m	Wed Jan 18 13:59:03 2017 -0500
@@ -57,6 +57,36 @@
     endif
   endif
 
+  if (isempty (x))
+    %% codepath for Matlab compatibility. empty x produces NaN output, but 
+    %% m f and c shape depends on size of x.  
+    if ((nd == 2) && (sz == [0 0]))
+      f = 0; %seems to always be a double even if x is single
+      if (isa (x, "single"))
+        m = single (NaN);
+        c = {(single (NaN (0, 1)))};
+      else
+        m = NaN;
+        c = {(NaN (0, 1))}; 
+      endif
+    else
+      sz (find ((sz ~= 1), 1)) = 1;
+      f = zeros (sz); %seems to always be a double even if x is single
+      if (isa (x, "single"))
+        m = single (NaN (sz));
+        c = cell (sz);
+        c(:) = {(single (NaN (1, 0)))};
+      else
+        m = NaN (sz);
+        c = cell (sz);
+        c(:) = {(NaN (1, 0))};
+      endif
+    endif
+  
+    return
+
+  endif
+  
   if (dim > nd)
     ## Special case of mode over non-existent dimension.
     m = x;
@@ -109,14 +139,14 @@
 
 %!test
 %! [m, f, c] = mode (toeplitz (1:5));
-%! assert (m, [1,2,2,2,1]);
-%! assert (f, [1,2,2,2,1]);
-%! assert (c, {[1;2;3;4;5],[2],[2;3],[2],[1;2;3;4;5]});
+%! assert (m, [1, 2, 2, 2, 1]);
+%! assert (f, [1, 2, 2, 2, 1]);
+%! assert (c, {[1; 2; 3; 4; 5],[2],[2; 3],[2],[1; 2; 3; 4; 5]});
 %!test
 %! [m, f, c] = mode (toeplitz (1:5), 2);
-%! assert (m, [1;2;2;2;1]);
-%! assert (f, [1;2;2;2;1]);
-%! assert (c, {[1;2;3;4;5];[2];[2;3];[2];[1;2;3;4;5]});
+%! assert (m, [1; 2; 2; 2; 1]);
+%! assert (f, [1; 2; 2; 2; 1]);
+%! assert (c, {[1; 2; 3; 4; 5];[2];[2; 3];[2];[1; 2; 3; 4; 5]});
 %!test
 %! a = sprandn (32, 32, 0.05);
 %! sp0 = sparse (0);
@@ -125,30 +155,30 @@
 %! assert (m, sparse (m2));
 %! assert (f, sparse (f2));
 %! c_exp(1:length (a)) = { sp0 };
-%! assert (c ,c_exp);
-%! assert (c2,c_exp);
+%! assert (c , c_exp);
+%! assert (c2, c_exp);
 
-%!assert (mode ([2,3,1,2,3,4],1),[2,3,1,2,3,4])
-%!assert (mode ([2,3,1,2,3,4],2),2)
-%!assert (mode ([2,3,1,2,3,4]),2)
-%!assert (mode (single ([2,3,1,2,3,4])), single (2))
-%!assert (mode (int8 ([2,3,1,2,3,4])), int8 (2))
+%!assert (mode ([2, 3, 1, 2, 3, 4], 1), [2, 3, 1, 2, 3, 4])
+%!assert (mode ([2, 3, 1, 2, 3, 4], 2), 2)
+%!assert (mode ([2, 3, 1, 2, 3, 4]), 2)
+%!assert (mode (single ([2, 3, 1, 2, 3, 4])), single (2))
+%!assert (mode (int8 ([2, 3, 1, 2, 3, 4])), int8 (2))
 
-%!assert (mode ([2;3;1;2;3;4],1),2)
-%!assert (mode ([2;3;1;2;3;4],2),[2;3;1;2;3;4])
-%!assert (mode ([2;3;1;2;3;4]),2)
+%!assert (mode ([2; 3; 1; 2; 3; 4], 1), 2)
+%!assert (mode ([2; 3; 1; 2; 3; 4], 2),[2; 3; 1; 2; 3; 4])
+%!assert (mode ([2; 3; 1; 2; 3; 4]), 2)
 
 %!test
 %! x = magic (3);
 %! [m, f, c] = mode (x, 3);
 %! assert (m, x);
-%! assert (f, ones (3,3));
+%! assert (f, ones (3, 3));
 %! assert (c, num2cell (x));
 
 %!shared x
-%! x(:,:,1) = toeplitz (1:3);
-%! x(:,:,2) = circshift (toeplitz (1:3), 1);
-%! x(:,:,3) = circshift (toeplitz (1:3), 2);
+%! x(:, :, 1) = toeplitz (1:3);
+%! x(:, :, 2) = circshift (toeplitz (1:3), 1);
+%! x(:, :, 3) = circshift (toeplitz (1:3), 2);
 %!test
 %! [m, f, c] = mode (x, 1);
 %! assert (reshape (m, [3, 3]), [1 1 1; 2 2 2; 1 1 1]);
@@ -174,6 +204,128 @@
 %! assert (c{2}, [1; 2; 3]);
 %! assert (c{3}, [1; 2; 3]);
 
+## Tests for empty input Matlab compatibility (bug #48690)
+%!test
+%! [m, f, c] = mode ([]);
+%! assert (m, NaN);
+%! assert (f, 0);
+%! assert (c, {(NaN (0, 1))});
+%!test
+%! [m, f, c] = mode (single ([]));
+%! assert (m, single (NaN));
+%! assert (f, double (0));
+%! assert (c, {(single (NaN (0, 1)))});
+%!test
+%! [m, f, c] = mode (ones (0, 1));
+%! assert (m, NaN);
+%! assert (f, 0);
+%! assert (c, {(NaN (1, 0))});
+%!test
+%! [m, f, c] = mode (ones (1, 0));
+%! assert (m, NaN);
+%! assert (f, 0);
+%! assert (c, {(NaN (1, 0))});
+%!test
+%! [m, f, c] = mode (single (ones (1, 0)));
+%! assert (m, single (NaN));
+%! assert (f, double (0));
+%! assert (c, {(single (NaN (1, 0)))});
+%!test
+%! [m, f, c] = mode (ones (2, 0));
+%! assert (m, NaN (1, 0));
+%! assert (f, zeros (1, 0));
+%! assert (c, cell (1, 0));
+%!test
+%! [m, f, c] = mode (ones (0, 2));
+%! assert (m, NaN (1, 2));
+%! assert (f, zeros (1, 2));
+%! assert (c, {(NaN (1, 0)),(NaN (1, 0))});
+%!test
+%! [m, f, c] = mode (single (ones (2, 0)));
+%! assert (m, single (NaN (1, 0)));
+%! assert (f, zeros (1, 0));
+%! assert (c, cell (1, 0));
+%!test
+%! [m, f, c] = mode (ones (0, 0, 1, 0));
+%! assert (m, NaN (1, 0, 1, 0));
+%! assert (f, zeros (1, 0, 1, 0));
+%! assert (c, cell (1, 0, 1, 0));
+%!test
+%! [m, f, c] = mode (ones (1, 1, 1, 0));
+%! assert (m, NaN (1, 1));
+%! assert (f, zeros (1, 1));
+%! assert (c, {(NaN (1, 0))});
+
+%!assert (mode (ones (0, 0, 0, 0)), NaN (1, 0, 0, 0))
+%!assert (mode (ones (0, 0, 0, 1)), NaN (1, 0, 0, 1))
+%!assert (mode (ones (0, 0, 0, 2)), NaN (1, 0, 0, 2))
+%!assert (mode (ones (0, 0, 1, 0)), NaN (1, 0, 1, 0))
+%!assert (mode (ones (0, 0, 1, 1)), NaN (1, 1, 1, 1))
+%!assert (mode (ones (0, 0, 1, 2)), NaN (1, 0, 1, 2))
+%!assert (mode (ones (0, 0, 2, 0)), NaN (1, 0, 2, 0))
+%!assert (mode (ones (0, 0, 2, 1)), NaN (1, 0, 2, 1))
+%!assert (mode (ones (0, 0, 2, 2)), NaN (1, 0, 2, 2))
+%!assert (mode (ones (0, 1, 0, 0)), NaN (1, 1, 0, 0))
+%!assert (mode (ones (0, 1, 0, 1)), NaN (1, 1, 0, 1))
+%!assert (mode (ones (0, 1, 0, 2)), NaN (1, 1, 0, 2))
+%!assert (mode (ones (0, 1, 1, 0)), NaN (1, 1, 1, 0))
+%!assert (mode (ones (0, 1, 1, 1)), NaN (1, 1, 1, 1))
+%!assert (mode (ones (0, 1, 1, 2)), NaN (1, 1, 1, 2))
+%!assert (mode (ones (0, 1, 2, 0)), NaN (1, 1, 2, 0))
+%!assert (mode (ones (0, 1, 2, 1)), NaN (1, 1, 2, 1))
+%!assert (mode (ones (0, 1, 2, 2)), NaN (1, 1, 2, 2))
+%!assert (mode (ones (0, 2, 0, 0)), NaN (1, 2, 0, 0))
+%!assert (mode (ones (0, 2, 0, 1)), NaN (1, 2, 0, 1))
+%!assert (mode (ones (0, 2, 0, 2)), NaN (1, 2, 0, 2))
+%!assert (mode (ones (0, 2, 1, 0)), NaN (1, 2, 1, 0))
+%!assert (mode (ones (0, 2, 1, 1)), NaN (1, 2, 1, 1))
+%!assert (mode (ones (0, 2, 1, 2)), NaN (1, 2, 1, 2))
+%!assert (mode (ones (0, 2, 2, 0)), NaN (1, 2, 2, 0))
+%!assert (mode (ones (0, 2, 2, 1)), NaN (1, 2, 2, 1))
+%!assert (mode (ones (0, 2, 2, 2)), NaN (1, 2, 2, 2))
+%!assert (mode (ones (1, 0, 0, 0)), NaN (1, 1, 0, 0))
+%!assert (mode (ones (1, 0, 0, 1)), NaN (1, 1, 0, 1))
+%!assert (mode (ones (1, 0, 0, 2)), NaN (1, 1, 0, 2))
+%!assert (mode (ones (1, 0, 1, 0)), NaN (1, 1, 1, 0))
+%!assert (mode (ones (1, 0, 1, 1)), NaN (1, 1, 1, 1))
+%!assert (mode (ones (1, 0, 1, 2)), NaN (1, 1, 1, 2))
+%!assert (mode (ones (1, 0, 2, 0)), NaN (1, 1, 2, 0))
+%!assert (mode (ones (1, 0, 2, 1)), NaN (1, 1, 2, 1))
+%!assert (mode (ones (1, 0, 2, 2)), NaN (1, 1, 2, 2))
+%!assert (mode (ones (1, 1, 0, 0)), NaN (1, 1, 1, 0))
+%!assert (mode (ones (1, 1, 0, 1)), NaN (1, 1, 1, 1))
+%!assert (mode (ones (1, 1, 0, 2)), NaN (1, 1, 1, 2))
+%!assert (mode (ones (1, 1, 1, 0)), NaN (1, 1, 1, 1))
+%!assert (mode (ones (1, 1, 2, 0)), NaN (1, 1, 1, 0))
+%!assert (mode (ones (1, 2, 0, 0)), NaN (1, 1, 0, 0))
+%!assert (mode (ones (1, 2, 0, 1)), NaN (1, 1, 0, 1))
+%!assert (mode (ones (1, 2, 0, 2)), NaN (1, 1, 0, 2))
+%!assert (mode (ones (1, 2, 1, 0)), NaN (1, 1, 1, 0))
+%!assert (mode (ones (1, 2, 2, 0)), NaN (1, 1, 2, 0))
+%!assert (mode (ones (2, 0, 0, 0)), NaN (1, 0, 0, 0))
+%!assert (mode (ones (2, 0, 0, 1)), NaN (1, 0, 0, 1))
+%!assert (mode (ones (2, 0, 0, 2)), NaN (1, 0, 0, 2))
+%!assert (mode (ones (2, 0, 1, 0)), NaN (1, 0, 1, 0))
+%!assert (mode (ones (2, 0, 1, 1)), NaN (1, 0, 1, 1))
+%!assert (mode (ones (2, 0, 1, 2)), NaN (1, 0, 1, 2))
+%!assert (mode (ones (2, 0, 2, 0)), NaN (1, 0, 2, 0))
+%!assert (mode (ones (2, 0, 2, 1)), NaN (1, 0, 2, 1))
+%!assert (mode (ones (2, 0, 2, 2)), NaN (1, 0, 2, 2))
+%!assert (mode (ones (2, 1, 0, 0)), NaN (1, 1, 0, 0))
+%!assert (mode (ones (2, 1, 0, 1)), NaN (1, 1, 0, 1))
+%!assert (mode (ones (2, 1, 0, 2)), NaN (1, 1, 0, 2))
+%!assert (mode (ones (2, 1, 1, 0)), NaN (1, 1, 1, 0))
+%!assert (mode (ones (2, 1, 2, 0)), NaN (1, 1, 2, 0))
+%!assert (mode (ones (2, 2, 0, 0)), NaN (1, 2, 0, 0))
+%!assert (mode (ones (2, 2, 0, 1)), NaN (1, 2, 0, 1))
+%!assert (mode (ones (2, 2, 0, 2)), NaN (1, 2, 0, 2))
+%!assert (mode (ones (2, 2, 1, 0)), NaN (1, 2, 1, 0))
+%!assert (mode (ones (2, 2, 2, 0)), NaN (1, 2, 2, 0))
+%!assert (mode (ones (1, 1, 0, 0, 0)), NaN (1, 1, 1, 0, 0))
+%!assert (mode (ones (1, 1, 1, 1, 0)), NaN (1, 1, 1, 1, 1))
+%!assert (mode (ones (2, 1, 1, 1, 0)), NaN (1, 1, 1, 1, 0))
+%!assert (mode (ones (1, 2, 1, 1, 0)), NaN (1, 1, 1, 1, 0))
+
 ## Test input validation
 %!error mode ()
 %!error mode (1, 2, 3)
diff -r 7c7a61c2e0ed -r 29c69084ea5a scripts/statistics/base/std.m
--- a/scripts/statistics/base/std.m	Tue Jan 17 17:42:27 2017 -0800
+++ b/scripts/statistics/base/std.m	Wed Jan 18 13:59:03 2017 -0500
@@ -91,12 +91,28 @@
   endif
 
   n = size (x, dim);
-  if (n == 1 || isempty (x))
+
+  if (isempty (x))
+    %% codepath for Matlab compatibility. empty x produces NaN output, but 
+    %% for ndim > 2, output depends on size of x.  
+    if ((nd == 2) && (max (sz) < 2))
+      retval = NaN;
+    else
+      sz (find ((sz ~= 1), 1)) = 1;
+      retval = NaN (sz);
+    endif
+
+    if (isa (x, "single"))
+        retval = single (retval);  
+    endif
+    
+  elseif (n == 1)
     if (isa (x, "single"))
       retval = zeros (sz, "single");
     else
       retval = zeros (sz);
     endif
+    
   else
     retval = sqrt (sumsq (center (x, dim), dim) / (n - 1 + opt));
   endif
@@ -116,16 +132,89 @@
 %!assert (std ([1 2], 1), 0.5, 5*eps)
 %!assert (std (1), 0)
 %!assert (std (single (1)), single (0))
-%!assert (std ([]), [])
-%!assert (std (ones (1,3,0,2)), ones (1,3,0,2))
 %!assert (std ([1 2 3], [], 3), [0 0 0])
 
+##tests for empty input Matlab compatibility (bug #48690)
+%!assert (std ([]), NaN)
+%!assert (std (single ([])), single (NaN))
+%!assert (std (ones (0, 0, 0, 0)), NaN (1, 0, 0, 0))
+%!assert (std (ones (0, 0, 0, 1)), NaN (1, 0, 0, 1))
+%!assert (std (ones (0, 0, 0, 2)), NaN (1, 0, 0, 2))
+%!assert (std (ones (0, 0, 1, 0)), NaN (1, 0, 1, 0))
+%!assert (std (ones (0, 0, 1, 1)), NaN (1, 1, 1, 1))
+%!assert (std (ones (0, 0, 1, 2)), NaN (1, 0, 1, 2))
+%!assert (std (ones (0, 0, 2, 0)), NaN (1, 0, 2, 0))
+%!assert (std (ones (0, 0, 2, 1)), NaN (1, 0, 2, 1))
+%!assert (std (ones (0, 0, 2, 2)), NaN (1, 0, 2, 2))
+%!assert (std (ones (0, 1, 0, 0)), NaN (1, 1, 0, 0))
+%!assert (std (ones (0, 1, 0, 1)), NaN (1, 1, 0, 1))
+%!assert (std (ones (0, 1, 0, 2)), NaN (1, 1, 0, 2))
+%!assert (std (ones (0, 1, 1, 0)), NaN (1, 1, 1, 0))
+%!assert (std (ones (0, 1, 1, 1)), NaN (1, 1, 1, 1))
+%!assert (std (ones (0, 1, 1, 2)), NaN (1, 1, 1, 2))
+%!assert (std (ones (0, 1, 2, 0)), NaN (1, 1, 2, 0))
+%!assert (std (ones (0, 1, 2, 1)), NaN (1, 1, 2, 1))
+%!assert (std (ones (0, 1, 2, 2)), NaN (1, 1, 2, 2))
+%!assert (std (ones (0, 2, 0, 0)), NaN (1, 2, 0, 0))
+%!assert (std (ones (0, 2, 0, 1)), NaN (1, 2, 0, 1))
+%!assert (std (ones (0, 2, 0, 2)), NaN (1, 2, 0, 2))
+%!assert (std (ones (0, 2, 1, 0)), NaN (1, 2, 1, 0))
+%!assert (std (ones (0, 2, 1, 1)), NaN (1, 2, 1, 1))
+%!assert (std (ones (0, 2, 1, 2)), NaN (1, 2, 1, 2))
+%!assert (std (ones (0, 2, 2, 0)), NaN (1, 2, 2, 0))
+%!assert (std (ones (0, 2, 2, 1)), NaN (1, 2, 2, 1))
+%!assert (std (ones (0, 2, 2, 2)), NaN (1, 2, 2, 2))
+%!assert (std (ones (1, 0, 0, 0)), NaN (1, 1, 0, 0))
+%!assert (std (ones (1, 0, 0, 1)), NaN (1, 1, 0, 1))
+%!assert (std (ones (1, 0, 0, 2)), NaN (1, 1, 0, 2))
+%!assert (std (ones (1, 0, 1, 0)), NaN (1, 1, 1, 0))
+%!assert (std (ones (1, 0, 1, 1)), NaN (1, 1, 1, 1))
+%!assert (std (ones (1, 0, 1, 2)), NaN (1, 1, 1, 2))
+%!assert (std (ones (1, 0, 2, 0)), NaN (1, 1, 2, 0))
+%!assert (std (ones (1, 0, 2, 1)), NaN (1, 1, 2, 1))
+%!assert (std (ones (1, 0, 2, 2)), NaN (1, 1, 2, 2))
+%!assert (std (ones (1, 1, 0, 0)), NaN (1, 1, 1, 0))
+%!assert (std (ones (1, 1, 0, 1)), NaN (1, 1, 1, 1))
+%!assert (std (ones (1, 1, 0, 2)), NaN (1, 1, 1, 2))
+%!assert (std (ones (1, 1, 1, 0)), NaN (1, 1, 1, 1))
+%!assert (std (ones (1, 1, 2, 0)), NaN (1, 1, 1, 0))
+%!assert (std (ones (1, 2, 0, 0)), NaN (1, 1, 0, 0))
+%!assert (std (ones (1, 2, 0, 1)), NaN (1, 1, 0, 1))
+%!assert (std (ones (1, 2, 0, 2)), NaN (1, 1, 0, 2))
+%!assert (std (ones (1, 2, 1, 0)), NaN (1, 1, 1, 0))
+%!assert (std (ones (1, 2, 2, 0)), NaN (1, 1, 2, 0))
+%!assert (std (ones (2, 0, 0, 0)), NaN (1, 0, 0, 0))
+%!assert (std (ones (2, 0, 0, 1)), NaN (1, 0, 0, 1))
+%!assert (std (ones (2, 0, 0, 2)), NaN (1, 0, 0, 2))
+%!assert (std (ones (2, 0, 1, 0)), NaN (1, 0, 1, 0))
+%!assert (std (ones (2, 0, 1, 1)), NaN (1, 0, 1, 1))
+%!assert (std (ones (2, 0, 1, 2)), NaN (1, 0, 1, 2))
+%!assert (std (ones (2, 0, 2, 0)), NaN (1, 0, 2, 0))
+%!assert (std (ones (2, 0, 2, 1)), NaN (1, 0, 2, 1))
+%!assert (std (ones (2, 0, 2, 2)), NaN (1, 0, 2, 2))
+%!assert (std (ones (2, 1, 0, 0)), NaN (1, 1, 0, 0))
+%!assert (std (ones (2, 1, 0, 1)), NaN (1, 1, 0, 1))
+%!assert (std (ones (2, 1, 0, 2)), NaN (1, 1, 0, 2))
+%!assert (std (ones (2, 1, 1, 0)), NaN (1, 1, 1, 0))
+%!assert (std (ones (2, 1, 2, 0)), NaN (1, 1, 2, 0))
+%!assert (std (ones (2, 2, 0, 0)), NaN (1, 2, 0, 0))
+%!assert (std (ones (2, 2, 0, 1)), NaN (1, 2, 0, 1))
+%!assert (std (ones (2, 2, 0, 2)), NaN (1, 2, 0, 2))
+%!assert (std (ones (2, 2, 1, 0)), NaN (1, 2, 1, 0))
+%!assert (std (ones (2, 2, 2, 0)), NaN (1, 2, 2, 0))
+%!assert (std (ones (1, 1, 0, 0, 0)), NaN (1, 1, 1, 0, 0))
+%!assert (std (ones (1, 1, 1, 1, 0)), NaN (1, 1, 1, 1, 1))
+%!assert (std (ones (2, 1, 1, 1, 0)), NaN (1, 1, 1, 1, 0))
+%!assert (std (ones (1, 2, 1, 1, 0)), NaN (1, 1, 1, 1, 0))
+%!assert (std (ones (1, 3, 0, 2)), NaN (1, 1, 0, 2)) 
+%!assert (std (single (ones (1, 3, 0, 2))), single (NaN (1, 1, 0, 2)))
+
 ## Test input validation
 %!error std ()
 %!error std (1, 2, 3, 4)
 %!error <X must be a numeric> std (['A'; 'B'])
 %!error <OPT must be 0 or 1> std (1, 2)
-%!error <DIM must be an integer> std (1, [], ones (2,2))
+%!error <DIM must be an integer> std (1, [], ones (2, 2))
 %!error <DIM must be an integer> std (1, [], 1.5)
 %!error <DIM must be .* a valid dimension> std (1, [], 0)
 
diff -r 7c7a61c2e0ed -r 29c69084ea5a scripts/statistics/base/var.m
--- a/scripts/statistics/base/var.m	Tue Jan 17 17:42:27 2017 -0800
+++ b/scripts/statistics/base/var.m	Wed Jan 18 13:59:03 2017 -0500
@@ -91,7 +91,22 @@
   endif
 
   n = size (x, dim);
-  if (n == 1)
+  
+  if (isempty (x))
+    %% codepath for Matlab compatibility. empty x produces NaN output, but 
+    %% for ndim > 2, output depends on size of x. 
+    if ((nd == 2) && (max (sz) < 2))
+      retval = NaN;
+    else
+      sz (find ((sz ~= 1), 1)) = 1;
+      retval = NaN (sz);
+    endif
+
+    if (isa (x, "single"))
+        retval = single (retval);  
+    endif
+  
+  elseif (n == 1)
     if (isa (x, "single"))
       retval = zeros (sz, "single");
     else
@@ -108,20 +123,94 @@
 
 %!assert (var (13), 0)
 %!assert (var (single (13)), single (0))
-%!assert (var ([1,2,3]), 1)
-%!assert (var ([1,2,3], 1), 2/3, eps)
-%!assert (var ([1,2,3], [], 1), [0,0,0])
-%!assert (var ([1,2,3], [], 3), [0,0,0])
+%!assert (var ([1, 2, 3]), 1)
+%!assert (var ([1, 2, 3], 1), 2/3, eps)
+%!assert (var ([1, 2, 3], [], 1), [0, 0, 0])
+%!assert (var ([1, 2, 3], [], 3), [0, 0, 0])
+
+##tests for empty input Matlab compatibility (bug #48690)
+%!assert (var ([]), NaN)
+%!assert (var (single ([])), single (NaN))
+%!assert (var (ones (0, 0, 0, 0)), NaN (1, 0, 0, 0))
+%!assert (var (ones (0, 0, 0, 1)), NaN (1, 0, 0, 1))
+%!assert (var (ones (0, 0, 0, 2)), NaN (1, 0, 0, 2))
+%!assert (var (ones (0, 0, 1, 0)), NaN (1, 0, 1, 0))
+%!assert (var (ones (0, 0, 1, 1)), NaN (1, 1, 1, 1))
+%!assert (var (ones (0, 0, 1, 2)), NaN (1, 0, 1, 2))
+%!assert (var (ones (0, 0, 2, 0)), NaN (1, 0, 2, 0))
+%!assert (var (ones (0, 0, 2, 1)), NaN (1, 0, 2, 1))
+%!assert (var (ones (0, 0, 2, 2)), NaN (1, 0, 2, 2))
+%!assert (var (ones (0, 1, 0, 0)), NaN (1, 1, 0, 0))
+%!assert (var (ones (0, 1, 0, 1)), NaN (1, 1, 0, 1))
+%!assert (var (ones (0, 1, 0, 2)), NaN (1, 1, 0, 2))
+%!assert (var (ones (0, 1, 1, 0)), NaN (1, 1, 1, 0))
+%!assert (var (ones (0, 1, 1, 1)), NaN (1, 1, 1, 1))
+%!assert (var (ones (0, 1, 1, 2)), NaN (1, 1, 1, 2))
+%!assert (var (ones (0, 1, 2, 0)), NaN (1, 1, 2, 0))
+%!assert (var (ones (0, 1, 2, 1)), NaN (1, 1, 2, 1))
+%!assert (var (ones (0, 1, 2, 2)), NaN (1, 1, 2, 2))
+%!assert (var (ones (0, 2, 0, 0)), NaN (1, 2, 0, 0))
+%!assert (var (ones (0, 2, 0, 1)), NaN (1, 2, 0, 1))
+%!assert (var (ones (0, 2, 0, 2)), NaN (1, 2, 0, 2))
+%!assert (var (ones (0, 2, 1, 0)), NaN (1, 2, 1, 0))
+%!assert (var (ones (0, 2, 1, 1)), NaN (1, 2, 1, 1))
+%!assert (var (ones (0, 2, 1, 2)), NaN (1, 2, 1, 2))
+%!assert (var (ones (0, 2, 2, 0)), NaN (1, 2, 2, 0))
+%!assert (var (ones (0, 2, 2, 1)), NaN (1, 2, 2, 1))
+%!assert (var (ones (0, 2, 2, 2)), NaN (1, 2, 2, 2))
+%!assert (var (ones (1, 0, 0, 0)), NaN (1, 1, 0, 0))
+%!assert (var (ones (1, 0, 0, 1)), NaN (1, 1, 0, 1))
+%!assert (var (ones (1, 0, 0, 2)), NaN (1, 1, 0, 2))
+%!assert (var (ones (1, 0, 1, 0)), NaN (1, 1, 1, 0))
+%!assert (var (ones (1, 0, 1, 1)), NaN (1, 1, 1, 1))
+%!assert (var (ones (1, 0, 1, 2)), NaN (1, 1, 1, 2))
+%!assert (var (ones (1, 0, 2, 0)), NaN (1, 1, 2, 0))
+%!assert (var (ones (1, 0, 2, 1)), NaN (1, 1, 2, 1))
+%!assert (var (ones (1, 0, 2, 2)), NaN (1, 1, 2, 2))
+%!assert (var (ones (1, 1, 0, 0)), NaN (1, 1, 1, 0))
+%!assert (var (ones (1, 1, 0, 1)), NaN (1, 1, 1, 1))
+%!assert (var (ones (1, 1, 0, 2)), NaN (1, 1, 1, 2))
+%!assert (var (ones (1, 1, 1, 0)), NaN (1, 1, 1, 1))
+%!assert (var (ones (1, 1, 2, 0)), NaN (1, 1, 1, 0))
+%!assert (var (ones (1, 2, 0, 0)), NaN (1, 1, 0, 0))
+%!assert (var (ones (1, 2, 0, 1)), NaN (1, 1, 0, 1))
+%!assert (var (ones (1, 2, 0, 2)), NaN (1, 1, 0, 2))
+%!assert (var (ones (1, 2, 1, 0)), NaN (1, 1, 1, 0))
+%!assert (var (ones (1, 2, 2, 0)), NaN (1, 1, 2, 0))
+%!assert (var (ones (2, 0, 0, 0)), NaN (1, 0, 0, 0))
+%!assert (var (ones (2, 0, 0, 1)), NaN (1, 0, 0, 1))
+%!assert (var (ones (2, 0, 0, 2)), NaN (1, 0, 0, 2))
+%!assert (var (ones (2, 0, 1, 0)), NaN (1, 0, 1, 0))
+%!assert (var (ones (2, 0, 1, 1)), NaN (1, 0, 1, 1))
+%!assert (var (ones (2, 0, 1, 2)), NaN (1, 0, 1, 2))
+%!assert (var (ones (2, 0, 2, 0)), NaN (1, 0, 2, 0))
+%!assert (var (ones (2, 0, 2, 1)), NaN (1, 0, 2, 1))
+%!assert (var (ones (2, 0, 2, 2)), NaN (1, 0, 2, 2))
+%!assert (var (ones (2, 1, 0, 0)), NaN (1, 1, 0, 0))
+%!assert (var (ones (2, 1, 0, 1)), NaN (1, 1, 0, 1))
+%!assert (var (ones (2, 1, 0, 2)), NaN (1, 1, 0, 2))
+%!assert (var (ones (2, 1, 1, 0)), NaN (1, 1, 1, 0))
+%!assert (var (ones (2, 1, 2, 0)), NaN (1, 1, 2, 0))
+%!assert (var (ones (2, 2, 0, 0)), NaN (1, 2, 0, 0))
+%!assert (var (ones (2, 2, 0, 1)), NaN (1, 2, 0, 1))
+%!assert (var (ones (2, 2, 0, 2)), NaN (1, 2, 0, 2))
+%!assert (var (ones (2, 2, 1, 0)), NaN (1, 2, 1, 0))
+%!assert (var (ones (2, 2, 2, 0)), NaN (1, 2, 2, 0))
+%!assert (var (ones (1, 1, 0, 0, 0)), NaN (1, 1, 1, 0, 0))
+%!assert (var (ones (1, 1, 1, 1, 0)), NaN (1, 1, 1, 1, 1))
+%!assert (var (ones (2, 1, 1, 1, 0)), NaN (1, 1, 1, 1, 0))
+%!assert (var (ones (1, 2, 1, 1, 0)), NaN (1, 1, 1, 1, 0))
+%!assert (var (ones (1, 3, 0, 2)), NaN (1, 1, 0, 2)) 
+%!assert (var (single (ones (1, 3, 0, 2))), single (NaN (1, 1, 0, 2)))
 
 ## Test input validation
 %!error var ()
-%!error var (1,2,3,4)
+%!error var (1, 2, 3, 4)
 %!error <X must be a numeric> var (['A'; 'B'])
 %!error <OPT must be 0 or 1> var (1, -1)
 %!error <FLAG must be 0 or 1> skewness (1, 2)
 %!error <FLAG must be 0 or 1> skewness (1, [1 0])
-%!error <DIM must be an integer> var (1, [], ones (2,2))
+%!error <DIM must be an integer> var (1, [], ones (2, 2))
 %!error <DIM must be an integer> var (1, [], 1.5)
 %!error <DIM must be .* a valid dimension> var (1, [], 0)
-%!error <X must not be empty> var ([], 1)
 
