6863
|
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 |
7016
|
7 ## the Free Software Foundation; either version 3 of the License, or (at |
|
8 ## your option) any later version. |
6863
|
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 |
7016
|
16 ## along with Octave; see the file COPYING. If not, see |
|
17 ## <http://www.gnu.org/licenses/>. |
6863
|
18 |
|
19 ## -*- texinfo -*- |
|
20 ## @deftypefn {Function File} {[@var{m}, @var{f}, @var{c}] =} mode (@var{x}, @var{dim}) |
|
21 ## Count the most frequently appearing value. @code{mode} counts the |
|
22 ## frequency along the first non-singleton dimension and if two or more |
|
23 ## values have te same frequency returns the smallest of the two in |
|
24 ## @var{m}. The dimension along which to count can be specified by the |
|
25 ## @var{dim} parameter. |
|
26 ## |
|
27 ## The variable @var{f} counts the frequency of each of the most frequently |
7007
|
28 ## occurring elements. The cell array @var{c} contains all of the elements |
6863
|
29 ## with the maximum frequency . |
|
30 ## @end deftypefn |
|
31 |
|
32 function [m, f, c] = mode (x, dim) |
|
33 |
|
34 if (nargin < 1 || nargin > 2) |
|
35 print_usage (); |
|
36 endif |
|
37 |
|
38 nd = ndims (x); |
|
39 sz = size (x); |
|
40 |
|
41 if (nargin != 2) |
|
42 ## Find the first non-singleton dimension. |
|
43 dim = 1; |
|
44 while (dim < nd + 1 && sz(dim) == 1) |
|
45 dim = dim + 1; |
|
46 endwhile |
|
47 if (dim > nd) |
|
48 dim = 1; |
|
49 endif |
|
50 else |
|
51 if (! (isscalar (dim) && dim == round (dim)) |
|
52 && dim > 0 |
|
53 && dim < (nd + 1)) |
|
54 error ("mode: dim must be an integer and valid dimension"); |
|
55 endif |
|
56 endif |
|
57 |
|
58 sz2 = sz; |
|
59 sz2 (dim) = 1; |
|
60 sz3 = ones (1, nd); |
|
61 sz3 (dim) = sz (dim); |
|
62 |
|
63 if (dim != 1) |
|
64 perm = [1 : nd]; |
|
65 perm(1) = dim; |
|
66 perm(dim) = 1; |
|
67 endif |
|
68 |
|
69 xs = sort (x, dim); |
|
70 t = cat (dim, true (sz2), diff (xs, 1, dim) != 0); |
|
71 if (issparse (x)) |
|
72 t2 = sparse (sz(1), sz(2)); |
|
73 else |
|
74 t2 = zeros (size (t)); |
|
75 endif |
|
76 |
|
77 if (dim != 1) |
|
78 t2 (permute (t != 0, perm)) = diff ([find(permute (t, perm)); prod(sz)+1]); |
|
79 f = max (ipermute (t2, perm), [], dim); |
|
80 xs = permute (xs, perm); |
|
81 else |
|
82 t2 (t) = diff ([find(t); prod(sz)+1]); |
|
83 f = max (t2, [], dim); |
|
84 endif |
|
85 |
|
86 c = cell (sz2); |
|
87 m = zeros (sz2); |
|
88 for i = 1 : prod (sz2) |
|
89 c {i} = xs (t2 (:, i) == f(i), i); |
|
90 m (i) = c{i}(1); |
|
91 endfor |
|
92 endfunction |
|
93 |
|
94 %!test |
|
95 %! [m, f, c] = mode (toeplitz (1:5)); |
|
96 %! assert (m, [1,2,2,2,1]); |
|
97 %! assert (f, [1,2,2,2,1]); |
|
98 %! assert (c, {[1;2;3;4;5],[2],[2;3],[2],[1;2;3;4;5]}); |
|
99 %!test |
|
100 %! [m, f, c] = mode (toeplitz (1:5), 2); |
|
101 %! assert (m, [1;2;2;2;1]); |
|
102 %! assert (f, [1;2;2;2;1]); |
|
103 %! assert (c, {[1;2;3;4;5];[2];[2;3];[2];[1;2;3;4;5]}); |
|
104 %!test |
|
105 %! a = sprandn (32, 32, 0.05); |
|
106 %! [m, f, c] = mode (a); |
|
107 %! [m2, f2, c2] = mode (full (a)); |
|
108 %! assert (m, m2); |
|
109 %! assert (f, f2); |
|
110 %! assert (c, c2); |