changeset 29176:7d262c7f8b1d

Allow quantile and prctile to work on dimensions greater than ndim (bug #59641) * prctile.m, quantile.m: Allow dim > ndim. Update tests. * NEWS: Note change.
author Nicholas R. Jankowski <jankowskin@asme.org>
date Tue, 08 Dec 2020 23:12:15 -0500
parents 0ce474653a35
children 4df23e9ed4dd
files NEWS scripts/statistics/prctile.m scripts/statistics/quantile.m
diffstat 3 files changed, 9 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/NEWS	Fri Dec 11 11:43:31 2020 -0500
+++ b/NEWS	Tue Dec 08 23:12:15 2020 -0500
@@ -176,6 +176,9 @@
 Matlab code, but for which Octave does not yet implement the
 functionality.  By default, this warning is enabled.
 
+- The functions `quantile` and `prctile` now permit operating on dimensions
+greater than `ndims(x)`.
+
 ### Alphabetical list of new functions added in Octave 7
 
 * `cospi`
--- a/scripts/statistics/prctile.m	Fri Dec 11 11:43:31 2020 -0500
+++ b/scripts/statistics/prctile.m	Tue Dec 08 23:12:15 2020 -0500
@@ -68,8 +68,7 @@
     ## Find the first non-singleton dimension.
     (dim = find (sz > 1, 1)) || (dim = 1);
   else
-    if (!(isscalar (dim) && dim == fix (dim))
-        || !(1 <= dim && dim <= nd))
+    if (!(isscalar (dim) && dim == fix (dim)) || !(1 <= dim))
       error ("prctile: DIM must be an integer and a valid dimension");
     endif
   endif
@@ -179,6 +178,8 @@
 %! qa = [0.1270; 0.2041; 0.6437; 0.6477; 0.9322];
 %! assert (q, qa, tol);
 
+%!assert (prctile ([1:10], 1, 3), [1:10])
+
 ## Test input validation
 %!error <Invalid call> prctile ()
 %!error prctile (['A'; 'B'], 10)
@@ -186,4 +187,3 @@
 %!error prctile (1:10, ones (2,2))
 %!error prctile (1, 1, 1.5)
 %!error prctile (1, 1, 0)
-%!error prctile (1, 1, 3)
--- a/scripts/statistics/quantile.m	Fri Dec 11 11:43:31 2020 -0500
+++ b/scripts/statistics/quantile.m	Tue Dec 08 23:12:15 2020 -0500
@@ -176,14 +176,13 @@
     ## Find the first non-singleton dimension.
     (dim = find (size (x) > 1, 1)) || (dim = 1);
   else
-    if (!(isscalar (dim) && dim == fix (dim))
-        || !(1 <= dim && dim <= ndims (x)))
+    if (!(isscalar (dim) && dim == fix (dim)) || !(1 <= dim))
       error ("quantile: DIM must be an integer and a valid dimension");
     endif
   endif
 
   ## Set the permutation vector.
-  perm = 1:ndims (x);
+  perm = 1:(max (ndims (x), dim));
   perm(1) = dim;
   perm(dim) = 1;
 
@@ -382,6 +381,7 @@
 %!assert <*54421> (quantile ([1:10]', 0.5, 2), [1:10]')
 %!assert <*54421> (quantile ([1:10], [0.25, 0.75]), [3, 8])
 %!assert <*54421> (quantile ([1:10], [0.25, 0.75]'), [3; 8])
+%!assert (quantile ([1:10], 1, 3), [1:10])
 
 ## Test input validation
 %!error <Invalid call> quantile ()
@@ -390,7 +390,6 @@
 %!error quantile (1:10, ones (2,2))
 %!error quantile (1, 1, 1.5)
 %!error quantile (1, 1, 0)
-%!error quantile (1, 1, 3)
 %!error quantile ((1:5)', 0.5, 1, 0)
 %!error quantile ((1:5)', 0.5, 1, 10)