comparison scripts/statistics/base/mode.m @ 6863:3c64128e621c

[project @ 2007-09-05 07:52:48 by dbateman]
author dbateman
date Wed, 05 Sep 2007 07:53:45 +0000
parents
children 6304d9ea0a30
comparison
equal deleted inserted replaced
6862:d63339cbb205 6863:3c64128e621c
1 ## Copyright (C) 2007 David Bateman
2 ##
3 ## This file is part of Octave.
4 ##
5 ## Octave is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 2, or (at your option)
8 ## any later version.
9 ##
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ## General Public License for more details.
14 ##
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING. If not, write to the Free
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 ## 02110-1301, USA.
19
20 ## -*- texinfo -*-
21 ## @deftypefn {Function File} {[@var{m}, @var{f}, @var{c}] =} mode (@var{x}, @var{dim})
22 ## Count the most frequently appearing value. @code{mode} counts the
23 ## frequency along the first non-singleton dimension and if two or more
24 ## values have te same frequency returns the smallest of the two in
25 ## @var{m}. The dimension along which to count can be specified by the
26 ## @var{dim} parameter.
27 ##
28 ## The variable @var{f} counts the frequency of each of the most frequently
29 ## occuring ellements. The cell array @var{c} contains all of the elements
30 ## with the maximum frequency .
31 ## @end deftypefn
32
33 function [m, f, c] = mode (x, dim)
34
35 if (nargin < 1 || nargin > 2)
36 print_usage ();
37 endif
38
39 nd = ndims (x);
40 sz = size (x);
41
42 if (nargin != 2)
43 ## Find the first non-singleton dimension.
44 dim = 1;
45 while (dim < nd + 1 && sz(dim) == 1)
46 dim = dim + 1;
47 endwhile
48 if (dim > nd)
49 dim = 1;
50 endif
51 else
52 if (! (isscalar (dim) && dim == round (dim))
53 && dim > 0
54 && dim < (nd + 1))
55 error ("mode: dim must be an integer and valid dimension");
56 endif
57 endif
58
59 sz2 = sz;
60 sz2 (dim) = 1;
61 sz3 = ones (1, nd);
62 sz3 (dim) = sz (dim);
63
64 if (dim != 1)
65 perm = [1 : nd];
66 perm(1) = dim;
67 perm(dim) = 1;
68 endif
69
70 xs = sort (x, dim);
71 t = cat (dim, true (sz2), diff (xs, 1, dim) != 0);
72 if (issparse (x))
73 t2 = sparse (sz(1), sz(2));
74 else
75 t2 = zeros (size (t));
76 endif
77
78 if (dim != 1)
79 t2 (permute (t != 0, perm)) = diff ([find(permute (t, perm)); prod(sz)+1]);
80 f = max (ipermute (t2, perm), [], dim);
81 xs = permute (xs, perm);
82 else
83 t2 (t) = diff ([find(t); prod(sz)+1]);
84 f = max (t2, [], dim);
85 endif
86
87 c = cell (sz2);
88 m = zeros (sz2);
89 for i = 1 : prod (sz2)
90 c {i} = xs (t2 (:, i) == f(i), i);
91 m (i) = c{i}(1);
92 endfor
93 endfunction
94
95 %!test
96 %! [m, f, c] = mode (toeplitz (1:5));
97 %! assert (m, [1,2,2,2,1]);
98 %! assert (f, [1,2,2,2,1]);
99 %! assert (c, {[1;2;3;4;5],[2],[2;3],[2],[1;2;3;4;5]});
100 %!test
101 %! [m, f, c] = mode (toeplitz (1:5), 2);
102 %! assert (m, [1;2;2;2;1]);
103 %! assert (f, [1;2;2;2;1]);
104 %! assert (c, {[1;2;3;4;5];[2];[2;3];[2];[1;2;3;4;5]});
105 %!test
106 %! a = sprandn (32, 32, 0.05);
107 %! [m, f, c] = mode (a);
108 %! [m2, f2, c2] = mode (full (a));
109 %! assert (m, m2);
110 %! assert (f, f2);
111 %! assert (c, c2);