# HG changeset patch # User David Bateman # Date 1205953323 14400 # Node ID 704b7a1098d0914143e9fe05efbd9f5632e7459c # Parent 48488cca0006b4c8e7ee8e32bcef9fa27ccce135 Fix for mode.m NDArrays and row vectors diff -r 48488cca0006 -r 704b7a1098d0 scripts/ChangeLog --- a/scripts/ChangeLog Wed Mar 19 14:12:57 2008 -0400 +++ b/scripts/ChangeLog Wed Mar 19 15:02:03 2008 -0400 @@ -1,3 +1,7 @@ +2008-03-19 Ben Abbott + + * statistics/base/mode.m: Add NDArray tests. + 2008-03-19 Jaroslav Hajek * statistics/distributions/exppdf.m, @@ -7,6 +11,8 @@ 2008-03-19 David Bateman + * statistics/base/mode.m: Fix for row vectors. + * plot/__scatter__.m: Modify for change of markersize in __go_draw_axes__.m and for compatibility. * plot/__go_draw_axes__.m: Don't divide the marker size by 6 diff -r 48488cca0006 -r 704b7a1098d0 scripts/statistics/base/mode.m --- a/scripts/statistics/base/mode.m Wed Mar 19 14:12:57 2008 -0400 +++ b/scripts/statistics/base/mode.m Wed Mar 19 15:02:03 2008 -0400 @@ -60,26 +60,26 @@ sz3 = ones (1, nd); sz3 (dim) = sz (dim); + if (issparse (x)) + t2 = sparse (sz(1), sz(2)); + else + t2 = zeros (sz); + endif + if (dim != 1) - perm = [1 : nd]; - perm(1) = dim; - perm(dim) = 1; + perm = [dim, 1:dim-1, dim+1:nd]; + t2 = permute (t2, perm); endif xs = sort (x, dim); t = cat (dim, true (sz2), diff (xs, 1, dim) != 0); - if (issparse (x)) - t2 = sparse (sz(1), sz(2)); - else - t2 = zeros (size (t)); - endif 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 @@ -112,3 +112,40 @@ %! assert (m, sparse (m2)); %! 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) +%!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); +%! x(:,:,2) = circshift (toeplitz (1:3), 1); +%! x(:,:,3) = circshift (toeplitz (1:3), 2); +%!test +%! [m, f, c] = mode (x, 1); +%! assert (reshape (m, [3, 3]), [1 1 1; 2 2 2; 1 1 1]) +%! assert (reshape (f, [3, 3]), [1 1 1; 2 2 2; 1 1 1]) +%! c = reshape (c, [3, 3]); +%! assert (c{1}, [1; 2; 3]) +%! assert (c{2}, 2) +%! assert (c{3}, [1; 2; 3]) +%!test +%! [m, f, c] = mode (x, 2); +%! assert (reshape (m, [3, 3]), [1 1 2; 2 1 1; 1 2 1]) +%! assert (reshape (f, [3, 3]), [1 1 2; 2 1 1; 1 2 1]) +%! c = reshape (c, [3, 3]); +%! assert (c{1}, [1; 2; 3]) +%! assert (c{2}, 2) +%! assert (c{3}, [1; 2; 3]) +%!test +%! [m, f, c] = mode (x, 3); +%! assert (reshape (m, [3, 3]), [1 2 1; 1 2 1; 1 2 1]) +%! assert (reshape (f, [3, 3]), [1 2 1; 1 2 1; 1 2 1]) +%! c = reshape (c, [3, 3]); +%! assert (c{1}, [1; 2; 3]) +%! assert (c{2}, [1; 2; 3]) +%! assert (c{3}, [1; 2; 3])