diff scripts/statistics/base/mode.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 4e32a1bb9096
children 7bd29d875af1
line wrap: on
line diff
--- a/scripts/statistics/base/mode.m	Sat May 07 14:30:20 2011 -0700
+++ b/scripts/statistics/base/mode.m	Sat May 07 14:52:08 2011 -0700
@@ -39,18 +39,15 @@
     print_usage ();
   endif
 
-  if (!isnumeric(x))
+  if (! (isnumeric (x) || islogical (x)))
     error ("mode: X must be a numeric vector or matrix");
   endif
 
   nd = ndims (x);
   sz = size (x);
-  if (nargin != 2)
+  if (nargin < 2)
     ## Find the first non-singleton dimension.
-    dim = find (sz > 1, 1);
-    if (isempty (dim))
-      dim = 1;
-    endif
+    (dim = find (sz > 1, 1)) || (dim = 1);
   else
     if (!(isscalar (dim) && dim == round (dim))
         || !(1 <= dim && dim <= nd))
@@ -78,11 +75,11 @@
   t = cat (dim, true (sz2), diff (xs, 1, dim) != 0);
 
   if (dim != 1)
-    t2 (permute (t != 0, perm)) = diff ([find(permute (t, perm))(:); prod(sz)+1]);
+    t2(permute (t != 0, perm)) = diff ([find(permute (t, perm))(:); prod(sz)+1]);
     f = max (ipermute (t2, perm), [], dim);
     xs = permute (xs, perm);
   else
-    t2 (t) = diff ([find(t)(:); prod(sz)+1]);
+    t2(t) = diff ([find(t)(:); prod(sz)+1]);
     f = max (t2, [], dim);
   endif
 
@@ -93,11 +90,12 @@
     m = zeros (sz2, class (x));
   endif
   for i = 1 : prod (sz2)
-    c{i} = xs (t2 (:, i) == f(i), i);
-    m (i) = c{i}(1);
+    c{i} = xs(t2(:, i) == f(i), i);
+    m(i) = c{i}(1);
   endfor
 endfunction
 
+
 %!test
 %! [m, f, c] = mode (toeplitz (1:5));
 %! assert (m, [1,2,2,2,1]);
@@ -116,13 +114,15 @@
 %! assert (f, sparse (f2));
 %! assert (c, cellfun (@(x) sparse (0), c2, 'uniformoutput', false));
 
-%!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)
-%!assert(mode([2,3,1,2,3,4]),2)
+%!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);
+%!assert(mode ([2,3,1,2,3,4]),2);
+%!assert(mode (single([2,3,1,2,3,4])), single(2));
+%!assert(mode (int8([2,3,1,2,3,4])), int8(2));
 
-%!assert(mode([2;3;1;2;3;4],1),2)
-%!assert(mode([2;3;1;2;3;4],2),[2;3;1;2;3;4])
-%!assert(mode([2;3;1;2;3;4]),2)
+%!assert(mode ([2;3;1;2;3;4],1),2);
+%!assert(mode ([2;3;1;2;3;4],2),[2;3;1;2;3;4]);
+%!assert(mode ([2;3;1;2;3;4]),2);
 
 %!shared x
 %! x(:,:,1) = toeplitz (1:3);
@@ -157,7 +157,7 @@
 %!error mode ()
 %!error mode (1, 2, 3)
 %!error mode ({1 2 3})
-%!error mode (true(1,3))
+%!error mode (['A'; 'B'])
 %!error mode (1, ones(2,2))
 %!error mode (1, 1.5)
 %!error mode (1, 0)