Mercurial > hg > octave-nkf
annotate scripts/statistics/base/mode.m @ 14149:f1ff06a1d73a stable
doc: Mention broadcasting in more relevant places.
author | Jordi Gutiérrez Hermoso <jordigh@octave.org> |
---|---|
date | Thu, 05 Jan 2012 10:34:04 -0500 |
parents | 72c96de7a403 |
children | f3d52523cde1 |
rev | line source |
---|---|
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
13279
diff
changeset
|
1 ## Copyright (C) 2007-2012 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 -*- | |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10669
diff
changeset
|
20 ## @deftypefn {Function File} {} mode (@var{x}) |
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10669
diff
changeset
|
21 ## @deftypefnx {Function File} {} mode (@var{x}, @var{dim}) |
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10669
diff
changeset
|
22 ## @deftypefnx {Function File} {[@var{m}, @var{f}, @var{c}] =} mode (@dots{}) |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
23 ## Compute the most frequently occurring value in a dataset (mode). |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10669
diff
changeset
|
24 ## @code{mode} determines the frequency of values along the first non-singleton |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
25 ## dimension and returns the value with the highest frequency. If two, or |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10669
diff
changeset
|
26 ## more, values have the same frequency @code{mode} returns the smallest. |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
27 ## |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
28 ## If the optional argument @var{dim} is given, operate along this dimension. |
6863 | 29 ## |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10669
diff
changeset
|
30 ## The return variable @var{f} is the number of occurrences of the mode in |
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10669
diff
changeset
|
31 ## in the dataset. The cell array @var{c} contains all of the elements |
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10669
diff
changeset
|
32 ## with the maximum frequency. |
12575
d0b799dafede
Grammarcheck files for 3.4.1 release.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
33 ## @seealso{mean, median} |
6863 | 34 ## @end deftypefn |
35 | |
36 function [m, f, c] = mode (x, dim) | |
37 | |
38 if (nargin < 1 || nargin > 2) | |
39 print_usage (); | |
40 endif | |
41 | |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12585
diff
changeset
|
42 if (! (isnumeric (x) || islogical (x))) |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
43 error ("mode: X must be a numeric vector or matrix"); |
10669
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
44 endif |
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
45 |
6863 | 46 nd = ndims (x); |
47 sz = size (x); | |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12585
diff
changeset
|
48 if (nargin < 2) |
6863 | 49 ## Find the first non-singleton dimension. |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12585
diff
changeset
|
50 (dim = find (sz > 1, 1)) || (dim = 1); |
6863 | 51 else |
13279
984359717d71
Use common code idiom for checking whether a double value is an integer.
Rik <octave@nomad.inbox5.com>
parents:
12930
diff
changeset
|
52 if (!(isscalar (dim) && dim == fix (dim)) |
11149
fe3c3dfc07eb
style fix: break lines before && and ||, not after
John W. Eaton <jwe@octave.org>
parents:
10687
diff
changeset
|
53 || !(1 <= dim && dim <= nd)) |
10669
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
54 error ("mode: DIM must be an integer and a valid dimension"); |
6863 | 55 endif |
56 endif | |
57 | |
58 sz2 = sz; | |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
59 sz2(dim) = 1; |
6863 | 60 sz3 = ones (1, nd); |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
61 sz3(dim) = sz(dim); |
6863 | 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) | |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12585
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 | |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12585
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 | |
12585
4e32a1bb9096
mode.m, quantile.m: Return output of same class as input.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
90 m = zeros (sz2, class (x)); |
7287 | 91 endif |
6863 | 92 for i = 1 : prod (sz2) |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12585
diff
changeset
|
93 c{i} = xs(t2(:, i) == f(i), i); |
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12585
diff
changeset
|
94 m(i) = c{i}(1); |
6863 | 95 endfor |
96 endfunction | |
97 | |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12585
diff
changeset
|
98 |
6863 | 99 %!test |
100 %! [m, f, c] = mode (toeplitz (1:5)); | |
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 %! [m, f, c] = mode (toeplitz (1:5), 2); | |
106 %! assert (m, [1;2;2;2;1]); | |
107 %! assert (f, [1;2;2;2;1]); | |
108 %! assert (c, {[1;2;3;4;5];[2];[2;3];[2];[1;2;3;4;5]}); | |
109 %!test | |
110 %! a = sprandn (32, 32, 0.05); | |
111 %! [m, f, c] = mode (a); | |
112 %! [m2, f2, c2] = mode (full (a)); | |
7287 | 113 %! assert (m, sparse (m2)); |
114 %! assert (f, sparse (f2)); | |
12930
7bd29d875af1
mode.m: Eliminate cellfun usage in assert tests
Rik <octave@nomad.inbox5.com>
parents:
12656
diff
changeset
|
115 %! c_exp(1:length(a)) = { sparse (0) }; |
7bd29d875af1
mode.m: Eliminate cellfun usage in assert tests
Rik <octave@nomad.inbox5.com>
parents:
12656
diff
changeset
|
116 %! assert (c ,c_exp); |
7bd29d875af1
mode.m: Eliminate cellfun usage in assert tests
Rik <octave@nomad.inbox5.com>
parents:
12656
diff
changeset
|
117 %! assert (c2,c_exp ); |
7606
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
118 |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12585
diff
changeset
|
119 %!assert(mode ([2,3,1,2,3,4],1),[2,3,1,2,3,4]); |
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12585
diff
changeset
|
120 %!assert(mode ([2,3,1,2,3,4],2),2); |
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12585
diff
changeset
|
121 %!assert(mode ([2,3,1,2,3,4]),2); |
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12585
diff
changeset
|
122 %!assert(mode (single([2,3,1,2,3,4])), single(2)); |
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12585
diff
changeset
|
123 %!assert(mode (int8([2,3,1,2,3,4])), int8(2)); |
7606
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
124 |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12585
diff
changeset
|
125 %!assert(mode ([2;3;1;2;3;4],1),2); |
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12585
diff
changeset
|
126 %!assert(mode ([2;3;1;2;3;4],2),[2;3;1;2;3;4]); |
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12585
diff
changeset
|
127 %!assert(mode ([2;3;1;2;3;4]),2); |
7606
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
128 |
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
129 %!shared x |
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
130 %! x(:,:,1) = toeplitz (1:3); |
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
131 %! 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
|
132 %! 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
|
133 %!test |
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
134 %! [m, f, c] = mode (x, 1); |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
135 %! assert (reshape (m, [3, 3]), [1 1 1; 2 2 2; 1 1 1]); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
136 %! assert (reshape (f, [3, 3]), [1 1 1; 2 2 2; 1 1 1]); |
7606
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
137 %! c = reshape (c, [3, 3]); |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
138 %! assert (c{1}, [1; 2; 3]); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
139 %! assert (c{2}, 2); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
140 %! assert (c{3}, [1; 2; 3]); |
7606
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
141 %!test |
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
142 %! [m, f, c] = mode (x, 2); |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
143 %! assert (reshape (m, [3, 3]), [1 1 2; 2 1 1; 1 2 1]); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
144 %! assert (reshape (f, [3, 3]), [1 1 2; 2 1 1; 1 2 1]); |
7606
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
145 %! c = reshape (c, [3, 3]); |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
146 %! assert (c{1}, [1; 2; 3]); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
147 %! assert (c{2}, 2); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
148 %! assert (c{3}, [1; 2; 3]); |
7606
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
149 %!test |
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
150 %! [m, f, c] = mode (x, 3); |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
151 %! assert (reshape (m, [3, 3]), [1 2 1; 1 2 1; 1 2 1]); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
152 %! assert (reshape (f, [3, 3]), [1 2 1; 1 2 1; 1 2 1]); |
7606
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
153 %! c = reshape (c, [3, 3]); |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
154 %! assert (c{1}, [1; 2; 3]); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
155 %! assert (c{2}, [1; 2; 3]); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
156 %! assert (c{3}, [1; 2; 3]); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
157 |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
158 %% Test input validation |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
159 %!error mode () |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
160 %!error mode (1, 2, 3) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
161 %!error mode ({1 2 3}) |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12585
diff
changeset
|
162 %!error mode (['A'; 'B']) |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
163 %!error mode (1, ones(2,2)) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
164 %!error mode (1, 1.5) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
165 %!error mode (1, 0) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
166 %!error mode (1, 3) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
167 |