# HG changeset patch
# User Nicholas R. Jankowski <jankowskin@asme.org>
# Date 1471361920 14400
#      Tue Aug 16 11:38:40 2016 -0400
# Node ID c24f386d50876f0c254b7e690cc0a80346d64e90
# Parent  5caa41d6b2c0a6e5fd4e32c498f299466bd5dab0
Edited mode.m so that emtpy input produces NaN with appropriate size, preserving single or double,  and handles all output variables. (bug #48690)

diff -r 5caa41d6b2c0 -r c24f386d5087 scripts/statistics/base/mode.m
--- a/scripts/statistics/base/mode.m	Mon Aug 15 16:02:55 2016 -0400
+++ b/scripts/statistics/base/mode.m	Tue Aug 16 11:38:40 2016 -0400
@@ -57,6 +57,40 @@
     endif
   endif
 
+  if (isempty (x))
+    %% codepath for Matlab Compatibility. empty x produces NaN output, but shape
+    %% of m f and c output depends on size of x.  tests included for 
+    %% compatibility with Matlab 2016a. Includes limited check for f and c, and 
+    %% all empty arrays up to dim4 for m, sizes of 0 1 or 2 in each dim.
+    %% TODO:  update this if later Matlab versions show more output consistency
+
+    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;
@@ -64,7 +98,7 @@
     c = num2cell (x);
     return;
   endif
-
+   
   sz2 = sz;
   sz2(dim) = 1;
   sz3 = ones (1, nd);
@@ -182,3 +216,124 @@
 %!error <DIM must be an integer> mode (1, 1.5)
 %!error <DIM must be .* a valid dimension> mode (1, 0)
 
+## Tests for empty input matlab compatibility (v2016a)
+%!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))
\ No newline at end of file
