# HG changeset patch
# User Nicholas R. Jankowski <jankowskin@asme.org>
# Date 1471291861 14400
#      Mon Aug 15 16:11:01 2016 -0400
# Node ID 8b80571efc50f52ceb19c1df088ef97c80fe620a
# Parent  c51c59bb8c178909160d8b01fed8c7a4ce44b23b
Edited median.m so that emtpy input produces NaN with appropriate size, preserving single or double. (bug #48690)

diff -r c51c59bb8c17 -r 8b80571efc50 scripts/statistics/base/median.m
--- a/scripts/statistics/base/median.m	Mon Aug 15 16:02:55 2016 -0400
+++ b/scripts/statistics/base/median.m	Mon Aug 15 16:11:01 2016 -0400
@@ -63,36 +63,52 @@
     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)
+      ## Find the first non-singleton dimension.
+      (dim = find (sz > 1, 1)) || (dim = 1);
+    else
+      if (! (isscalar (dim) && dim == fix (dim) && dim > 0))
+        error ("median: DIM must be an integer and a valid dimension");
+      endif
+    endif
 
-  nd = ndims (x);
-  sz = size (x);
-  if (nargin < 2)
-    ## Find the first non-singleton dimension.
-    (dim = find (sz > 1, 1)) || (dim = 1);
-  else
-    if (! (isscalar (dim) && dim == fix (dim) && dim > 0))
-      error ("median: DIM must be an integer and a valid dimension");
+    n = size (x, dim);
+    k = floor ((n+1) / 2);
+  
+    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 (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
-  endif
-
-  n = size (x, dim);
-  k = floor ((n+1) / 2);
-  if (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);
+    ## Inject NaNs where needed, to be consistent with Matlab.
+    if (isfloat (x))
+      retval(any (isnan (x), dim)) = NaN;
     endif
-  endif
-  ## Inject NaNs where needed, to be consistent with Matlab.
-  if (isfloat (x))
-    retval(any (isnan (x), dim)) = NaN;
-  endif
-
+    
 endfunction
 
 
@@ -121,6 +137,84 @@
 %!assert (median (a, 4), x(:, :, :, 3))
 %!assert (median (b, 3), (y(:, :, 3, :) + y(:, :, 4, :))/2)
 
+##tests for empty input matlab compatibility (v2016a)
+%!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,7 +226,6 @@
 %!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, 1.5)
 %!error <DIM must be .* a valid dimension> median (1, 0)
