Mercurial > octave-nkf
annotate scripts/statistics/base/mode.m @ 12575:d0b799dafede
Grammarcheck files for 3.4.1 release.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Mon, 04 Apr 2011 15:33:46 -0700 |
parents | c792872f8942 |
children | 4e32a1bb9096 |
rev | line source |
---|---|
11523 | 1 ## Copyright (C) 2007-2011 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 | |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
42 if (!isnumeric(x)) |
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); | |
48 if (nargin != 2) | |
49 ## Find the first non-singleton dimension. | |
10669
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
50 dim = find (sz > 1, 1); |
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
51 if (isempty (dim)) |
6863 | 52 dim = 1; |
53 endif | |
54 else | |
11149
fe3c3dfc07eb
style fix: break lines before && and ||, not after
John W. Eaton <jwe@octave.org>
parents:
10687
diff
changeset
|
55 if (!(isscalar (dim) && dim == round (dim)) |
fe3c3dfc07eb
style fix: break lines before && and ||, not after
John W. Eaton <jwe@octave.org>
parents:
10687
diff
changeset
|
56 || !(1 <= dim && dim <= nd)) |
10669
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
57 error ("mode: DIM must be an integer and a valid dimension"); |
6863 | 58 endif |
59 endif | |
60 | |
61 sz2 = sz; | |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
62 sz2(dim) = 1; |
6863 | 63 sz3 = ones (1, nd); |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
64 sz3(dim) = sz(dim); |
6863 | 65 |
7606
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
66 if (issparse (x)) |
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
67 t2 = sparse (sz(1), sz(2)); |
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
68 else |
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
69 t2 = zeros (sz); |
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
70 endif |
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
71 |
6863 | 72 if (dim != 1) |
7606
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
73 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
|
74 t2 = permute (t2, perm); |
6863 | 75 endif |
76 | |
77 xs = sort (x, dim); | |
78 t = cat (dim, true (sz2), diff (xs, 1, dim) != 0); | |
79 | |
80 if (dim != 1) | |
7606
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
81 t2 (permute (t != 0, perm)) = diff ([find(permute (t, perm))(:); prod(sz)+1]); |
6863 | 82 f = max (ipermute (t2, perm), [], dim); |
83 xs = permute (xs, perm); | |
84 else | |
7606
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
85 t2 (t) = diff ([find(t)(:); prod(sz)+1]); |
6863 | 86 f = max (t2, [], dim); |
87 endif | |
88 | |
89 c = cell (sz2); | |
7287 | 90 if (issparse (x)) |
91 m = sparse (sz2(1), sz2(2)); | |
92 else | |
93 m = zeros (sz2); | |
94 endif | |
6863 | 95 for i = 1 : prod (sz2) |
7208 | 96 c{i} = xs (t2 (:, i) == f(i), i); |
6863 | 97 m (i) = c{i}(1); |
98 endfor | |
99 endfunction | |
100 | |
101 %!test | |
102 %! [m, f, c] = mode (toeplitz (1:5)); | |
103 %! assert (m, [1,2,2,2,1]); | |
104 %! assert (f, [1,2,2,2,1]); | |
105 %! assert (c, {[1;2;3;4;5],[2],[2;3],[2],[1;2;3;4;5]}); | |
106 %!test | |
107 %! [m, f, c] = mode (toeplitz (1:5), 2); | |
108 %! assert (m, [1;2;2;2;1]); | |
109 %! assert (f, [1;2;2;2;1]); | |
110 %! assert (c, {[1;2;3;4;5];[2];[2;3];[2];[1;2;3;4;5]}); | |
111 %!test | |
112 %! a = sprandn (32, 32, 0.05); | |
113 %! [m, f, c] = mode (a); | |
114 %! [m2, f2, c2] = mode (full (a)); | |
7287 | 115 %! assert (m, sparse (m2)); |
116 %! assert (f, sparse (f2)); | |
11191
01ddaedd6ad5
Reverse changeset b1f4bdc276b6. Use all lower case for "uniformoutput" option.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
117 %! 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
|
118 |
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
119 %!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
|
120 %!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
|
121 %!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
|
122 |
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
123 %!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
|
124 %!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
|
125 %!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
|
126 |
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
127 %!shared x |
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
128 %! x(:,:,1) = toeplitz (1:3); |
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
129 %! 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
|
130 %! 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
|
131 %!test |
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
132 %! [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
|
133 %! 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
|
134 %! 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
|
135 %! c = reshape (c, [3, 3]); |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
136 %! assert (c{1}, [1; 2; 3]); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
137 %! assert (c{2}, 2); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
138 %! 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
|
139 %!test |
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
140 %! [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
|
141 %! 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
|
142 %! 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
|
143 %! c = reshape (c, [3, 3]); |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
144 %! assert (c{1}, [1; 2; 3]); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
145 %! assert (c{2}, 2); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
146 %! 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
|
147 %!test |
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
148 %! [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
|
149 %! 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
|
150 %! 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
|
151 %! c = reshape (c, [3, 3]); |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
152 %! assert (c{1}, [1; 2; 3]); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
153 %! assert (c{2}, [1; 2; 3]); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
154 %! assert (c{3}, [1; 2; 3]); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
155 |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
156 %% Test input validation |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
157 %!error mode () |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
158 %!error mode (1, 2, 3) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
159 %!error mode ({1 2 3}) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
160 %!error mode (true(1,3)) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
161 %!error mode (1, ones(2,2)) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
162 %!error mode (1, 1.5) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
163 %!error mode (1, 0) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
164 %!error mode (1, 3) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
165 |