Mercurial > hg > octave-lyh
annotate scripts/statistics/base/mode.m @ 8920:eb63fbe60fab
update copyright notices
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Sat, 07 Mar 2009 10:41:27 -0500 |
parents | 704b7a1098d0 |
children | 1bf0ce0930be |
rev | line source |
---|---|
8920 | 1 ## Copyright (C) 2007, 2008 David Bateman |
6863 | 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 | |
7606
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
63 if (issparse (x)) |
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
64 t2 = sparse (sz(1), sz(2)); |
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
65 else |
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
66 t2 = zeros (sz); |
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
67 endif |
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
68 |
6863 | 69 if (dim != 1) |
7606
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
70 perm = [dim, 1:dim-1, dim+1:nd]; |
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
71 t2 = permute (t2, perm); |
6863 | 72 endif |
73 | |
74 xs = sort (x, dim); | |
75 t = cat (dim, true (sz2), diff (xs, 1, dim) != 0); | |
76 | |
77 if (dim != 1) | |
7606
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
78 t2 (permute (t != 0, perm)) = diff ([find(permute (t, perm))(:); prod(sz)+1]); |
6863 | 79 f = max (ipermute (t2, perm), [], dim); |
80 xs = permute (xs, perm); | |
81 else | |
7606
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
82 t2 (t) = diff ([find(t)(:); prod(sz)+1]); |
6863 | 83 f = max (t2, [], dim); |
84 endif | |
85 | |
86 c = cell (sz2); | |
7287 | 87 if (issparse (x)) |
88 m = sparse (sz2(1), sz2(2)); | |
89 else | |
90 m = zeros (sz2); | |
91 endif | |
6863 | 92 for i = 1 : prod (sz2) |
7208 | 93 c{i} = xs (t2 (:, i) == f(i), i); |
6863 | 94 m (i) = c{i}(1); |
95 endfor | |
96 endfunction | |
97 | |
98 %!test | |
99 %! [m, f, c] = mode (toeplitz (1:5)); | |
100 %! assert (m, [1,2,2,2,1]); | |
101 %! assert (f, [1,2,2,2,1]); | |
102 %! assert (c, {[1;2;3;4;5],[2],[2;3],[2],[1;2;3;4;5]}); | |
103 %!test | |
104 %! [m, f, c] = mode (toeplitz (1:5), 2); | |
105 %! assert (m, [1;2;2;2;1]); | |
106 %! assert (f, [1;2;2;2;1]); | |
107 %! assert (c, {[1;2;3;4;5];[2];[2;3];[2];[1;2;3;4;5]}); | |
108 %!test | |
109 %! a = sprandn (32, 32, 0.05); | |
110 %! [m, f, c] = mode (a); | |
111 %! [m2, f2, c2] = mode (full (a)); | |
7287 | 112 %! assert (m, sparse (m2)); |
113 %! assert (f, sparse (f2)); | |
114 %! assert (c, cellfun (@(x) sparse (0), c2, 'UniformOutput', false)); | |
7606
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
115 |
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
116 %!assert(mode([2,3,1,2,3,4],1),[2,3,1,2,3,4]) |
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
117 %!assert(mode([2,3,1,2,3,4],2),2) |
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
118 %!assert(mode([2,3,1,2,3,4]),2) |
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
119 |
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
120 %!assert(mode([2;3;1;2;3;4],1),2) |
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
121 %!assert(mode([2;3;1;2;3;4],2),[2;3;1;2;3;4]) |
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
122 %!assert(mode([2;3;1;2;3;4]),2) |
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
123 |
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
124 %!shared x |
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
125 %! x(:,:,1) = toeplitz (1:3); |
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
126 %! x(:,:,2) = circshift (toeplitz (1:3), 1); |
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
127 %! x(:,:,3) = circshift (toeplitz (1:3), 2); |
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
128 %!test |
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
129 %! [m, f, c] = mode (x, 1); |
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
130 %! assert (reshape (m, [3, 3]), [1 1 1; 2 2 2; 1 1 1]) |
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
131 %! assert (reshape (f, [3, 3]), [1 1 1; 2 2 2; 1 1 1]) |
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
132 %! c = reshape (c, [3, 3]); |
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
133 %! assert (c{1}, [1; 2; 3]) |
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
134 %! assert (c{2}, 2) |
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
135 %! assert (c{3}, [1; 2; 3]) |
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
136 %!test |
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
137 %! [m, f, c] = mode (x, 2); |
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
138 %! assert (reshape (m, [3, 3]), [1 1 2; 2 1 1; 1 2 1]) |
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
139 %! assert (reshape (f, [3, 3]), [1 1 2; 2 1 1; 1 2 1]) |
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
140 %! c = reshape (c, [3, 3]); |
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
141 %! assert (c{1}, [1; 2; 3]) |
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
142 %! assert (c{2}, 2) |
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
143 %! assert (c{3}, [1; 2; 3]) |
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
144 %!test |
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
145 %! [m, f, c] = mode (x, 3); |
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
146 %! assert (reshape (m, [3, 3]), [1 2 1; 1 2 1; 1 2 1]) |
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
147 %! assert (reshape (f, [3, 3]), [1 2 1; 1 2 1; 1 2 1]) |
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
148 %! c = reshape (c, [3, 3]); |
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
149 %! assert (c{1}, [1; 2; 3]) |
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
150 %! assert (c{2}, [1; 2; 3]) |
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
151 %! assert (c{3}, [1; 2; 3]) |