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 5 files changed, 71 insertions(+), 39 deletions(-) [+]
line wrap: on
line diff
--- 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  <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	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 \
--- 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
-## <http://www.gnu.org/licenses/>.
-
-## -*- 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]);
--- 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 \
--- /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
+## <http://www.gnu.org/licenses/>.
+
+## -*- 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))