Mercurial > hg > octave-avbm
changeset 12531:33716f289ba5
Overhaul runlength.m
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Sat, 19 Mar 2011 14:32:43 -0700 |
parents | d70c99028ba3 |
children | 06cebc991966 |
files | scripts/ChangeLog scripts/general/module.mk scripts/general/runlength.m scripts/statistics/base/module.mk scripts/statistics/base/runlength.m |
diffstat | 4 files changed, 35 insertions(+), 3 deletions(-) [+] |
line wrap: on
line diff
--- a/scripts/ChangeLog +++ b/scripts/ChangeLog @@ -1,3 +1,10 @@ +2010-03-19 Rik <octave@nomad.inbox5.com> + + * general/module.mk, statistics/base/module.mk: Move runlength.m + to statistics/base directory. + * statistics/base/runlength.m: Add input validation and tests. + Improve docstring. + 2010-03-19 Rik <octave@nomad.inbox5.com> * statistics/models/logistic_regression.m: Do not split function
--- a/scripts/general/module.mk +++ b/scripts/general/module.mk @@ -70,7 +70,6 @@ general/repmat.m \ general/rot90.m \ general/rotdim.m \ - general/runlength.m \ general/saveobj.m \ general/shift.m \ general/shiftdim.m \
--- a/scripts/statistics/base/module.mk +++ b/scripts/statistics/base/module.mk @@ -28,6 +28,7 @@ statistics/base/range.m \ statistics/base/ranks.m \ statistics/base/run_count.m \ + statistics/base/runlength.m \ statistics/base/skewness.m \ statistics/base/spearman.m \ statistics/base/statistics.m \
rename from scripts/general/runlength.m rename to scripts/statistics/base/runlength.m --- a/scripts/general/runlength.m +++ b/scripts/statistics/base/runlength.m @@ -17,7 +17,7 @@ ## <http://www.gnu.org/licenses/>. ## -*- texinfo -*- -## @deftypefn {Function File} {} runlength (@var{x}) +## @deftypefn {Function File} {[count, value] =} runlength (@var{x}) ## Find the lengths of all sequences of common values. Return the ## vector of lengths and the value that was repeated. ## @@ -30,9 +30,34 @@ ## @end deftypefn function [count, value] = runlength (x) + if (nargin != 1) + print_usage (); + endif + + if (!isnumeric (x) || !isvector (x)) + error ("runlength: X must be a numeric vector"); + endif + + if (iscolumn (x)) + x = x.'; + endif + idx = [find(x(1:end-1) != x(2:end)), length(x)]; - value = x(idx); count = diff ([0 idx]); + if (nargout == 2) + value = x(idx); + endif endfunction %!assert (runlength([2 2 0 4 4 4 0 1 1 1 1]), [2 1 3 1 4]); +%!assert (runlength([2 2 0 4 4 4 0 1 1 1 1]'), [2 1 3 1 4]); +%!test +%! [c, v] = runlength ([2 2 0 4 4 4 0 1 1 1 1]); +%! assert (c, [2 1 3 1 4]); +%! assert (v, [2 0 4 0 1]); + +%% Test input validation +%!error runlength () +%!error runlength (1, 2) +%!error runlength (true(1,2)) +%!error runlength (ones(2,2))