# HG changeset patch # User Rik # Date 1300570363 25200 # Node ID 33716f289ba59407835d7e2c969ced18ac63c6c7 # Parent d70c99028ba33e5a6d9de8b55d34648cef8730dc Overhaul runlength.m diff -r d70c99028ba3 -r 33716f289ba5 scripts/ChangeLog --- a/scripts/ChangeLog Sat Mar 19 13:53:38 2011 -0700 +++ b/scripts/ChangeLog Sat Mar 19 14:32:43 2011 -0700 @@ -1,3 +1,10 @@ +2010-03-19 Rik + + * 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 * statistics/models/logistic_regression.m: Do not split function diff -r d70c99028ba3 -r 33716f289ba5 scripts/general/module.mk --- a/scripts/general/module.mk Sat Mar 19 13:53:38 2011 -0700 +++ b/scripts/general/module.mk Sat Mar 19 14:32:43 2011 -0700 @@ -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 \ diff -r d70c99028ba3 -r 33716f289ba5 scripts/general/runlength.m --- a/scripts/general/runlength.m Sat Mar 19 13:53:38 2011 -0700 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,38 +0,0 @@ -## Copyright (C) 2005-2011 Paul Kienzle -## -## This file is part of Octave. -## -## Octave is free software; you can redistribute it and/or modify it -## under the terms of the GNU General Public License as published by -## the Free Software Foundation; either version 3 of the License, or (at -## your option) any later version. -## -## Octave is distributed in the hope that it will be useful, but -## WITHOUT ANY WARRANTY; without even the implied warranty of -## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -## General Public License for more details. -## -## You should have received a copy of the GNU General Public License -## along with Octave; see the file COPYING. If not, see -## . - -## -*- texinfo -*- -## @deftypefn {Function File} {} runlength (@var{x}) -## Find the lengths of all sequences of common values. Return the -## vector of lengths and the value that was repeated. -## -## @example -## @group -## runlength ([2, 2, 0, 4, 4, 4, 0, 1, 1, 1, 1]) -## @result{} [2, 1, 3, 1, 4] -## @end group -## @end example -## @end deftypefn - -function [count, value] = runlength (x) - idx = [find(x(1:end-1) != x(2:end)), length(x)]; - value = x(idx); - count = diff ([0 idx]); -endfunction - -%!assert (runlength([2 2 0 4 4 4 0 1 1 1 1]), [2 1 3 1 4]); diff -r d70c99028ba3 -r 33716f289ba5 scripts/statistics/base/module.mk --- a/scripts/statistics/base/module.mk Sat Mar 19 13:53:38 2011 -0700 +++ b/scripts/statistics/base/module.mk Sat Mar 19 14:32:43 2011 -0700 @@ -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 \ diff -r d70c99028ba3 -r 33716f289ba5 scripts/statistics/base/runlength.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/scripts/statistics/base/runlength.m Sat Mar 19 14:32:43 2011 -0700 @@ -0,0 +1,63 @@ +## Copyright (C) 2005-2011 Paul Kienzle +## +## This file is part of Octave. +## +## Octave is free software; you can redistribute it and/or modify it +## under the terms of the GNU General Public License as published by +## the Free Software Foundation; either version 3 of the License, or (at +## your option) any later version. +## +## Octave is distributed in the hope that it will be useful, but +## WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +## General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with Octave; see the file COPYING. If not, see +## . + +## -*- texinfo -*- +## @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. +## +## @example +## @group +## runlength ([2, 2, 0, 4, 4, 4, 0, 1, 1, 1, 1]) +## @result{} [2, 1, 3, 1, 4] +## @end group +## @end example +## @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)]; + 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))