Mercurial > octave
changeset 21364:03ff9371596b
mode.m: Allow operation over non-existent dimension (bug #33523).
* mode.m: Code special case to detect non-existent dimension and
return original matrix immediately. Add input validation tests.
author | Rik <rik@octave.org> |
---|---|
date | Fri, 26 Feb 2016 13:40:04 -0800 |
parents | 8cfd1b47d49f |
children | 5b9618f4f841 |
files | scripts/statistics/base/mode.m |
diffstat | 1 files changed, 21 insertions(+), 9 deletions(-) [+] |
line wrap: on
line diff
--- a/scripts/statistics/base/mode.m Fri Feb 26 13:13:38 2016 -0800 +++ b/scripts/statistics/base/mode.m Fri Feb 26 13:40:04 2016 -0800 @@ -52,12 +52,19 @@ ## 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) && dim > 0)) error ("mode: DIM must be an integer and a valid dimension"); endif endif + if (dim > nd) + ## Special case of mode over non-existent dimension + m = x; + f = ones (size (x)); + c = num2cell (x); + return; + endif + sz2 = sz; sz2(dim) = 1; sz3 = ones (1, nd); @@ -118,7 +125,7 @@ %! assert (f, sparse (f2)); %! c_exp(1:length (a)) = { sp0 }; %! assert (c ,c_exp); -%! assert (c2,c_exp ); +%! assert (c2,c_exp); %!assert (mode ([2,3,1,2,3,4],1),[2,3,1,2,3,4]) %!assert (mode ([2,3,1,2,3,4],2),2) @@ -130,6 +137,13 @@ %!assert (mode ([2;3;1;2;3;4],2),[2;3;1;2;3;4]) %!assert (mode ([2;3;1;2;3;4]),2) +%!test +%! x = magic (3); +%! [m, f, c] = mode (x, 3); +%! assert (m, x); +%! assert (f, ones (3,3)); +%! assert (c, num2cell (x)); + %!shared x %! x(:,:,1) = toeplitz (1:3); %! x(:,:,2) = circshift (toeplitz (1:3), 1); @@ -162,10 +176,8 @@ ## Test input validation %!error mode () %!error mode (1, 2, 3) -%!error mode ({1 2 3}) -%!error mode (['A'; 'B']) -%!error mode (1, ones (2,2)) -%!error mode (1, 1.5) -%!error mode (1, 0) -%!error mode (1, 3) +%!error <X must be a numeric> mode ({1 2 3}) +%!error <DIM must be an integer> mode (1, ones (2,2)) +%!error <DIM must be an integer> mode (1, 1.5) +%!error <DIM must be .* a valid dimension> mode (1, 0)