comparison scripts/statistics/base/runlength.m @ 12656:6b2f14af2360

Overhaul functions in statistics/base directory. Widen input validation to accept logicals. Return correct class of output, e.g., 'single' depending on class of input. Correct or add tests for above. * center.m, cov.m, kendall.m, mean.m, meansq.m, median.m, mode.m, prctile.m, quantile.m, ranks.m, run_count.m, runlength.m, spearman.m, statistics.m, std.m, var.m, logistic_inv.m: Overhaul as described above * corrcoef.m: Overhaul + remove input validation already done by cov(). * cor.m, logit.m, ppplot.m, table.m: Only align test blocks. * gls.m, ols.m: Only correct class of output, no logical inputs for regression. * histc.m: Only change spacing of code to be uniform. * iqr.m: Overhaul + 2X speedup by calling empirical_inv just once. * kurtosis.m: Overhaul + replace repmat instances with center(). * mahalanobis.m: Overhaul + use bsxfun for centering data. * moment.m: Overhaul + replace repmat instances with center(). * probit.m, range.m: Redo input validation and add tests. * skewness.m: Overhaul + replace repmat instances with center(). * zscore.m: Overhaul + replace repmat instances with center() + use bsxfun.
author Rik <octave@nomad.inbox5.com>
date Sat, 07 May 2011 14:52:08 -0700
parents 33716f289ba5
children 72c96de7a403
comparison
equal deleted inserted replaced
12655:52d79740a76c 12656:6b2f14af2360
28 ## @end group 28 ## @end group
29 ## @end example 29 ## @end example
30 ## @end deftypefn 30 ## @end deftypefn
31 31
32 function [count, value] = runlength (x) 32 function [count, value] = runlength (x)
33
33 if (nargin != 1) 34 if (nargin != 1)
34 print_usage (); 35 print_usage ();
35 endif 36 endif
36 37
37 if (!isnumeric (x) || !isvector (x)) 38 if (!(isnumeric (x) || islogical (x)) || !isvector (x))
38 error ("runlength: X must be a numeric vector"); 39 error ("runlength: X must be a numeric vector");
39 endif 40 endif
40 41
41 if (iscolumn (x)) 42 if (iscolumn (x))
42 x = x.'; 43 x = x.';
45 idx = [find(x(1:end-1) != x(2:end)), length(x)]; 46 idx = [find(x(1:end-1) != x(2:end)), length(x)];
46 count = diff ([0 idx]); 47 count = diff ([0 idx]);
47 if (nargout == 2) 48 if (nargout == 2)
48 value = x(idx); 49 value = x(idx);
49 endif 50 endif
51
50 endfunction 52 endfunction
53
51 54
52 %!assert (runlength([2 2 0 4 4 4 0 1 1 1 1]), [2 1 3 1 4]); 55 %!assert (runlength([2 2 0 4 4 4 0 1 1 1 1]), [2 1 3 1 4]);
53 %!assert (runlength([2 2 0 4 4 4 0 1 1 1 1]'), [2 1 3 1 4]); 56 %!assert (runlength([2 2 0 4 4 4 0 1 1 1 1]'), [2 1 3 1 4]);
54 %!test 57 %!test
55 %! [c, v] = runlength ([2 2 0 4 4 4 0 1 1 1 1]); 58 %! [c, v] = runlength ([2 2 0 4 4 4 0 1 1 1 1]);
57 %! assert (v, [2 0 4 0 1]); 60 %! assert (v, [2 0 4 0 1]);
58 61
59 %% Test input validation 62 %% Test input validation
60 %!error runlength () 63 %!error runlength ()
61 %!error runlength (1, 2) 64 %!error runlength (1, 2)
62 %!error runlength (true(1,2)) 65 %!error runlength (['A'; 'B'])
63 %!error runlength (ones(2,2)) 66 %!error runlength (ones(2,2))