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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
2 ##
3922
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3456
diff changeset
3 ## This file is part of Octave.
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3456
diff changeset
4 ##
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3456
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3456
diff changeset
6 ## under the terms of the GNU General Public License as published by
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6547
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6547
diff changeset
8 ## your option) any later version.
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
9 ##
3922
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3456
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
13 ## General Public License for more details.
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
14 ##
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
15 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6547
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6547
diff changeset
17 ## <http://www.gnu.org/licenses/>.
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
18
3453
71d2e09c15a2 [project @ 2000-01-18 08:32:09 by jwe]
jwe
parents: 3426
diff changeset
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
71d2e09c15a2 [project @ 2000-01-18 08:32:09 by jwe]
jwe
parents: 3426
diff changeset
28 ## @end deftypefn
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
29
3456
434790acb067 [project @ 2000-01-19 06:58:51 by jwe]
jwe
parents: 3453
diff changeset
30 ## Author: FL <Friedrich.Leisch@ci.tuwien.ac.at>
434790acb067 [project @ 2000-01-19 06:58:51 by jwe]
jwe
parents: 3453
diff changeset
31 ## Description: Count upward runs
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
32
4885
28ab079d8f0e [project @ 2004-04-30 04:21:33 by jwe]
jwe
parents: 3922
diff changeset
33 function retval = run_count (x, n, dim)
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
34
4885
28ab079d8f0e [project @ 2004-04-30 04:21:33 by jwe]
jwe
parents: 3922
diff changeset
35 if (nargin != 2 && nargin != 3)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5307
diff changeset
36 print_usage ();
4885
28ab079d8f0e [project @ 2004-04-30 04:21:33 by jwe]
jwe
parents: 3922
diff changeset
37 endif
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
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
28ab079d8f0e [project @ 2004-04-30 04:21:33 by jwe]
jwe
parents: 3922
diff changeset
47 nd = ndims (x);
28ab079d8f0e [project @ 2004-04-30 04:21:33 by jwe]
jwe
parents: 3922
diff changeset
48 sz = size (x);
28ab079d8f0e [project @ 2004-04-30 04:21:33 by jwe]
jwe
parents: 3922
diff changeset
49 if (nargin != 3)
4886
54b076a24718 [project @ 2004-04-30 04:31:51 by jwe]
jwe
parents: 4885
diff changeset
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
28ab079d8f0e [project @ 2004-04-30 04:21:33 by jwe]
jwe
parents: 3922
diff changeset
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
28ab079d8f0e [project @ 2004-04-30 04:21:33 by jwe]
jwe
parents: 3922
diff changeset
56 endif
28ab079d8f0e [project @ 2004-04-30 04:21:33 by jwe]
jwe
parents: 3922
diff changeset
57 endif
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
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
28ab079d8f0e [project @ 2004-04-30 04:21:33 by jwe]
jwe
parents: 3922
diff changeset
60 if (dim != 1)
28ab079d8f0e [project @ 2004-04-30 04:21:33 by jwe]
jwe
parents: 3922
diff changeset
61 perm = [1 : nd];
4886
54b076a24718 [project @ 2004-04-30 04:31:51 by jwe]
jwe
parents: 4885
diff changeset
62 perm(1) = dim;
54b076a24718 [project @ 2004-04-30 04:31:51 by jwe]
jwe
parents: 4885
diff changeset
63 perm(dim) = 1;
4885
28ab079d8f0e [project @ 2004-04-30 04:21:33 by jwe]
jwe
parents: 3922
diff changeset
64 x = permute (x, perm);
28ab079d8f0e [project @ 2004-04-30 04:21:33 by jwe]
jwe
parents: 3922
diff changeset
65 endif
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
66
4885
28ab079d8f0e [project @ 2004-04-30 04:21:33 by jwe]
jwe
parents: 3922
diff changeset
67 sz = size (x);
28ab079d8f0e [project @ 2004-04-30 04:21:33 by jwe]
jwe
parents: 3922
diff changeset
68 idx = cell ();
28ab079d8f0e [project @ 2004-04-30 04:21:33 by jwe]
jwe
parents: 3922
diff changeset
69 for i = 1 : nd
4886
54b076a24718 [project @ 2004-04-30 04:31:51 by jwe]
jwe
parents: 4885
diff changeset
70 idx{i} = 1 : sz(i);
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
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
28ab079d8f0e [project @ 2004-04-30 04:21:33 by jwe]
jwe
parents: 3922
diff changeset
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
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
75
4885
28ab079d8f0e [project @ 2004-04-30 04:21:33 by jwe]
jwe
parents: 3922
diff changeset
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
54b076a24718 [project @ 2004-04-30 04:31:51 by jwe]
jwe
parents: 4885
diff changeset
78 tmp = tmp(idx{:});
4885
28ab079d8f0e [project @ 2004-04-30 04:21:33 by jwe]
jwe
parents: 3922
diff changeset
79
4886
54b076a24718 [project @ 2004-04-30 04:31:51 by jwe]
jwe
parents: 4885
diff changeset
80 sz(1) = n;
4885
28ab079d8f0e [project @ 2004-04-30 04:21:33 by jwe]
jwe
parents: 3922
diff changeset
81 retval = zeros (sz);
4886
54b076a24718 [project @ 2004-04-30 04:31:51 by jwe]
jwe
parents: 4885
diff changeset
82 for k = 1 : (n-1)
54b076a24718 [project @ 2004-04-30 04:31:51 by jwe]
jwe
parents: 4885
diff changeset
83 idx{1} = k;
54b076a24718 [project @ 2004-04-30 04:31:51 by jwe]
jwe
parents: 4885
diff changeset
84 retval(idx{:}) = sum (tmp == k);
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
85 endfor
4886
54b076a24718 [project @ 2004-04-30 04:31:51 by jwe]
jwe
parents: 4885
diff changeset
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
28ab079d8f0e [project @ 2004-04-30 04:21:33 by jwe]
jwe
parents: 3922
diff changeset
88
28ab079d8f0e [project @ 2004-04-30 04:21:33 by jwe]
jwe
parents: 3922
diff changeset
89 if (dim != 1)
28ab079d8f0e [project @ 2004-04-30 04:21:33 by jwe]
jwe
parents: 3922
diff changeset
90 retval = ipermute (retval, perm);
28ab079d8f0e [project @ 2004-04-30 04:21:33 by jwe]
jwe
parents: 3922
diff changeset
91 endif
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
92
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
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