# HG changeset patch
# User Nicholas R. Jankowski <jankowskin@asme.org>
# Date 1471290210 14400
#      Mon Aug 15 15:43:30 2016 -0400
# Node ID 9f5c997edd01cba7499775198c5d4d5dcb5c9b33
# Parent  8a456af24e6b58fafe997623696b2d69779d9582
Edited var.m so that emtpy input produces NaN with appropriate size, preserving single or double (bug #48690)

diff -r 8a456af24e6b -r 9f5c997edd01 scripts/statistics/base/var.m
--- a/scripts/statistics/base/var.m	Sun Jul 17 17:03:30 2016 -0400
+++ b/scripts/statistics/base/var.m	Mon Aug 15 15:43:30 2016 -0400
@@ -91,7 +91,26 @@
   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.  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 (isa (x, "single"))
+        retval = single (retval);  
+    endif
+  
+  elseif (n == 1)
     if (isa (x, "single"))
       retval = zeros (sz, "single");
     else
@@ -113,6 +132,84 @@
 %!assert (var ([1,2,3], [], 1), [0,0,0])
 %!assert (var ([1,2,3], [], 3), [0,0,0])
 
+##tests for empty input matlab compatibility (v2016a)
+%!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)
@@ -122,6 +219,4 @@
 %!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, [], 1.5)
-%!error <DIM must be .* a valid dimension> var (1, [], 0)
-%!error <X must not be empty> var ([], 1)
-
+%!error <DIM must be .* a valid dimension> var (1, [], 0)
\ No newline at end of file
