Mercurial > octave-nkf
annotate scripts/statistics/base/run_count.m @ 20595:c1a6c31ac29a
eliminate more simple uses of error_state
* ov-classdef.cc: Eliminate simple uses of error_state.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Tue, 06 Oct 2015 00:20:02 -0400 |
parents | d9341b422488 |
children |
rev | line source |
---|---|
19731
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
1 ## Copyright (C) 1995-2015 Friedrich Leisch |
3426 | 2 ## |
3922 | 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. | |
3426 | 9 ## |
3922 | 10 ## Octave is distributed in the hope that it will be useful, but |
3200 | 11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
3426 | 13 ## General Public License for more details. |
14 ## | |
3200 | 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/>. | |
3200 | 18 |
3453 | 19 ## -*- texinfo -*- |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
20 ## @deftypefn {Function File} {} run_count (@var{x}, @var{n}) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
21 ## @deftypefnx {Function File} {} run_count (@var{x}, @var{n}, @var{dim}) |
20209
d9341b422488
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19867
diff
changeset
|
22 ## Count the upward runs along the first non-singleton dimension of @var{x} |
d9341b422488
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19867
diff
changeset
|
23 ## of length 1, 2, @dots{}, @var{n}-1 and greater than or equal to @var{n}. |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
24 ## |
20209
d9341b422488
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19867
diff
changeset
|
25 ## If the optional argument @var{dim} is given then operate along this |
d9341b422488
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19867
diff
changeset
|
26 ## dimension. |
d9341b422488
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19867
diff
changeset
|
27 ## @seealso{runlength} |
3453 | 28 ## @end deftypefn |
3200 | 29 |
3456 | 30 ## Author: FL <Friedrich.Leisch@ci.tuwien.ac.at> |
31 ## Description: Count upward runs | |
3200 | 32 |
4885 | 33 function retval = run_count (x, n, dim) |
3426 | 34 |
4885 | 35 if (nargin != 2 && nargin != 3) |
6046 | 36 print_usage (); |
4885 | 37 endif |
3426 | 38 |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
39 if (! (isnumeric (x) || islogical (x))) |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
40 error ("run_count: 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:
10549
diff
changeset
|
41 endif |
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
42 |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
43 if (!(isscalar (n) && n == fix (n) && n > 0)) |
10669
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
44 error ("run_count: N must be a positive integer"); |
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
45 endif |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
46 |
4885 | 47 nd = ndims (x); |
48 sz = size (x); | |
49 if (nargin != 3) | |
4886 | 50 ## Find the first non-singleton dimension. |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
51 (dim = find (sz > 1, 1)) || (dim = 1); |
4885 | 52 else |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
53 if (!(isscalar (dim) && dim == fix (dim)) |
11149
fe3c3dfc07eb
style fix: break lines before && and ||, not after
John W. Eaton <jwe@octave.org>
parents:
10669
diff
changeset
|
54 || !(1 <= dim && dim <= nd)) |
10669
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
55 error ("run_count: DIM must be an integer and a valid dimension"); |
4885 | 56 endif |
57 endif | |
3200 | 58 |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
59 ## Algorithm works on rows. Permute array if necessary, ipermute back at end |
4885 | 60 if (dim != 1) |
61 perm = [1 : nd]; | |
4886 | 62 perm(1) = dim; |
63 perm(dim) = 1; | |
4885 | 64 x = permute (x, perm); |
65 endif | |
3200 | 66 |
4885 | 67 sz = size (x); |
68 idx = cell (); | |
69 for i = 1 : nd | |
4886 | 70 idx{i} = 1 : sz(i); |
3200 | 71 endfor |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
72 c = sz(1); |
4885 | 73 tmp = zeros ([c + 1, sz(2 : end)]); |
10540
952d4df5b686
Eliminate NaN*ones and Inf*ones constructs and just use Nan() and Inf()
Rik <code@nomad.inbox5.com>
parents:
9245
diff
changeset
|
74 infvec = Inf ([1, sz(2 : end)]); |
3426 | 75 |
4885 | 76 ind = find (diff ([infvec; x; -infvec]) < 0); |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
77 tmp(ind(2:end) - 1) = diff (ind); |
4886 | 78 tmp = tmp(idx{:}); |
4885 | 79 |
4886 | 80 sz(1) = n; |
4885 | 81 retval = zeros (sz); |
4886 | 82 for k = 1 : (n-1) |
83 idx{1} = k; | |
84 retval(idx{:}) = sum (tmp == k); | |
3200 | 85 endfor |
4886 | 86 idx{1} = n; |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
87 retval(idx{:}) = sum (tmp >= n); |
4885 | 88 |
89 if (dim != 1) | |
90 retval = ipermute (retval, perm); | |
91 endif | |
3426 | 92 |
3200 | 93 endfunction |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
94 |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
95 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
96 %!assert (run_count (magic (3), 4), [1,0,1;1,0,1;0,1,0;0,0,0]) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
97 %!assert (run_count (magic (3), 4, 2), [1,0,1;1,0,1;0,1,0;0,0,0]') |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
98 %!assert (run_count (5:-1:1, 5), [5, 0, 0, 0, 0]) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
99 %!assert (run_count (ones (3), 4), [0,0,0;0,0,0;1,1,1;0,0,0]) |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
100 |
19867
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19731
diff
changeset
|
101 ## Test input validation |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
102 %!error run_count () |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
103 %!error run_count (1) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
104 %!error run_count (1, 2, 3, 4) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
105 %!error run_count ({1, 2}, 3) |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
106 %!error run_count (['A'; 'A'; 'B'], 3) |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
107 %!error run_count (1:5, ones (2,2)) |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
108 %!error run_count (1:5, 1.5) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
109 %!error run_count (1:5, -2) |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
110 %!error run_count (1:5, 3, ones (2,2)) |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
111 %!error run_count (1:5, 3, 1.5) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
112 %!error run_count (1:5, 3, 0) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
113 |