Mercurial > octave
annotate scripts/statistics/base/mode.m @ 20803:c22206c1a88f
eliminate return statements after calls to print_usage
* __ilu__.cc, besselj.cc, conv2.cc, error.cc, getgrent.cc,
getpwent.cc, help.cc, load-path.cc, sparse.cc:
Eliminate return statements after calls to print_usage.
* mode.m: Fix test.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Fri, 04 Dec 2015 14:06:41 -0500 |
parents | d9341b422488 |
children | 516bb87ea72e |
rev | line source |
---|---|
19697
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
1 ## Copyright (C) 2007-2015 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). |
20174
d9341b422488
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
24 ## |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10669
diff
changeset
|
25 ## @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
|
26 ## 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
|
27 ## 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
|
28 ## |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
29 ## If the optional argument @var{dim} is given, operate along this dimension. |
6863 | 30 ## |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10669
diff
changeset
|
31 ## The return variable @var{f} is the number of occurrences of the mode in |
20174
d9341b422488
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
32 ## the dataset. |
d9341b422488
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
33 ## |
d9341b422488
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
34 ## The cell array @var{c} contains all of the elements with the maximum |
d9341b422488
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
35 ## frequency. |
12575
d0b799dafede
Grammarcheck files for 3.4.1 release.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
36 ## @seealso{mean, median} |
6863 | 37 ## @end deftypefn |
38 | |
39 function [m, f, c] = mode (x, dim) | |
40 | |
41 if (nargin < 1 || nargin > 2) | |
42 print_usage (); | |
43 endif | |
44 | |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12585
diff
changeset
|
45 if (! (isnumeric (x) || islogical (x))) |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
46 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
|
47 endif |
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
48 |
6863 | 49 nd = ndims (x); |
50 sz = size (x); | |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12585
diff
changeset
|
51 if (nargin < 2) |
6863 | 52 ## Find the first non-singleton dimension. |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12585
diff
changeset
|
53 (dim = find (sz > 1, 1)) || (dim = 1); |
6863 | 54 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
|
55 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
|
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) | |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12585
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 | |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12585
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 | |
12585
4e32a1bb9096
mode.m, quantile.m: Return output of same class as input.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
93 m = zeros (sz2, class (x)); |
7287 | 94 endif |
6863 | 95 for i = 1 : prod (sz2) |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12585
diff
changeset
|
96 c{i} = xs(t2(:, i) == f(i), i); |
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12585
diff
changeset
|
97 m(i) = c{i}(1); |
6863 | 98 endfor |
99 endfunction | |
100 | |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12585
diff
changeset
|
101 |
6863 | 102 %!test |
103 %! [m, f, c] = mode (toeplitz (1:5)); | |
104 %! assert (m, [1,2,2,2,1]); | |
105 %! assert (f, [1,2,2,2,1]); | |
106 %! assert (c, {[1;2;3;4;5],[2],[2;3],[2],[1;2;3;4;5]}); | |
107 %!test | |
108 %! [m, f, c] = mode (toeplitz (1:5), 2); | |
109 %! assert (m, [1;2;2;2;1]); | |
110 %! assert (f, [1;2;2;2;1]); | |
111 %! assert (c, {[1;2;3;4;5];[2];[2;3];[2];[1;2;3;4;5]}); | |
112 %!test | |
113 %! a = sprandn (32, 32, 0.05); | |
20803
c22206c1a88f
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20174
diff
changeset
|
114 %! sp0 = sparse (0); |
6863 | 115 %! [m, f, c] = mode (a); |
116 %! [m2, f2, c2] = mode (full (a)); | |
7287 | 117 %! assert (m, sparse (m2)); |
118 %! assert (f, sparse (f2)); | |
20803
c22206c1a88f
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20174
diff
changeset
|
119 %! c_exp(1:length (a)) = { sp0 }; |
12930
7bd29d875af1
mode.m: Eliminate cellfun usage in assert tests
Rik <octave@nomad.inbox5.com>
parents:
12656
diff
changeset
|
120 %! assert (c ,c_exp); |
7bd29d875af1
mode.m: Eliminate cellfun usage in assert tests
Rik <octave@nomad.inbox5.com>
parents:
12656
diff
changeset
|
121 %! assert (c2,c_exp ); |
7606
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
122 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
123 %!assert (mode ([2,3,1,2,3,4],1),[2,3,1,2,3,4]) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
124 %!assert (mode ([2,3,1,2,3,4],2),2) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
125 %!assert (mode ([2,3,1,2,3,4]),2) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
126 %!assert (mode (single ([2,3,1,2,3,4])), single (2)) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
127 %!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
|
128 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
129 %!assert (mode ([2;3;1;2;3;4],1),2) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
130 %!assert (mode ([2;3;1;2;3;4],2),[2;3;1;2;3;4]) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
131 %!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
|
132 |
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
133 %!shared x |
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
134 %! x(:,:,1) = toeplitz (1:3); |
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
135 %! 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
|
136 %! 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
|
137 %!test |
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
138 %! [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
|
139 %! 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
|
140 %! 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
|
141 %! c = reshape (c, [3, 3]); |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
142 %! assert (c{1}, [1; 2; 3]); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
143 %! assert (c{2}, 2); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
144 %! 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
|
145 %!test |
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
146 %! [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
|
147 %! 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
|
148 %! 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
|
149 %! c = reshape (c, [3, 3]); |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
150 %! assert (c{1}, [1; 2; 3]); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
151 %! assert (c{2}, 2); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
152 %! 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
|
153 %!test |
704b7a1098d0
Fix for mode.m NDArrays and row vectors
David Bateman <dbateman@free.fr>
parents:
7287
diff
changeset
|
154 %! [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
|
155 %! 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
|
156 %! 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
|
157 %! c = reshape (c, [3, 3]); |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
158 %! assert (c{1}, [1; 2; 3]); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
159 %! assert (c{2}, [1; 2; 3]); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
160 %! assert (c{3}, [1; 2; 3]); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
161 |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
162 ## Test input validation |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
163 %!error mode () |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
164 %!error mode (1, 2, 3) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
165 %!error mode ({1 2 3}) |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12585
diff
changeset
|
166 %!error mode (['A'; 'B']) |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
167 %!error mode (1, ones (2,2)) |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
168 %!error mode (1, 1.5) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
169 %!error mode (1, 0) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
170 %!error mode (1, 3) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
171 |