changeset 31937:162669cd13c8

median.m: Correct odd k orientation in omitnan codepath. * median.m: Force odd k values in omitnan path into column vector to avoid size mismatch error in n-D array inputs. Add BIST to catch error case.
author Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
date Mon, 27 Mar 2023 10:38:25 -0400
parents 9d5ccdbae3ad
children 8e82a7fc21aa
files scripts/statistics/median.m
diffstat 1 files changed, 8 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/statistics/median.m	Sun Mar 26 14:51:44 2023 -0700
+++ b/scripts/statistics/median.m	Mon Mar 27 10:38:25 2023 -0400
@@ -94,7 +94,7 @@
 ## NaN values from the calculation using any of the previously specified input
 ## argument combinations.  The default value for @var{nanflag} is
 ## @qcode{"includenan"} which keeps NaN values in the calculation.  To
-## exclude NaN values set the value of @var{nanflag} to @qcode{"omitnan"}. 
+## exclude NaN values set the value of @var{nanflag} to @qcode{"omitnan"}.
 ## The output will still contain NaN values if @var{x} consists of all NaN
 ## values in the operating dimension.
 ##
@@ -346,7 +346,7 @@
 
       ## Grab kth value, k possibly different for each column
       if (any (m_idx_odd(:)))
-        x_idx_odd = sub2ind (szx, k(m_idx_odd), find (m_idx_odd));
+        x_idx_odd = sub2ind (szx, k(m_idx_odd)(:), find (m_idx_odd));
         m(m_idx_odd) = x(x_idx_odd);
       endif
       if (any (m_idx_even(:)))
@@ -618,6 +618,12 @@
 %!assert <*35679> (median (b, 3), (y(:, :, 3, :) + y(:, :, 4, :))/2)
 %!shared   ## Clear shared to prevent variable echo for any later test failures
 
+## Test n-dimensional arrays with odd non-NaN data points
+%!test
+%! x = ones(15,1,4);
+%! x([13,15],1,:) = NaN;
+%! assert (median (x, 1, "omitnan"), ones (1,1,4))
+
 ## Test non-floating point types
 %!assert (median ([true, false]), true)
 %!assert (median (logical ([])), false)