mode returns a wrong answer for row vectors
Ben Abbott
bpabbott at mac.com
Sun Mar 16 11:39:20 CDT 2008
On Mar 16, 2008, at 10:38 AM, David Bateman wrote:
> Ben Abbott wrote:
>> There has been some work on reconciling a similar problem for
>> calculation of quantiles on the thread below.
>>
>> http://www.nabble.com/Statistics-function-incorrectly-computing-median-tc14644111.html
>
> mode already has something like that treatment.. This isn't the issue.
> The problem was that I forgot to reshape the matrix t2 in mode.m if
> dim!=1 whereas the later code assume it was reshaped.. You'll see that
>
> a = floor(3*rand(5,4))
> mode (a, 2)
>
> also fails for the same reason. The attached patch should address
> this,
> and also added a couple of additional tests.
Thanks David. You resolved the problem a couple of hours before I'd
finished my approach.
I applied your changes manually to the last version of octave I am
able to the original 3.0.0 release (I'm unable to build from the
current sources).
It wasn't clear to me if your method would work for N-d arrays, so I
added some additional tests.
%!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])
I may have done something wrong, but the last test fails for me.
Can you verify? If so, shall I post the changeset for the approach I
took?
Ben
More information about the Bug-octave
mailing list