]> Creatis software - CreaPhase.git/blob - octave_packages/m/statistics/base/run_count.m
update packages
[CreaPhase.git] / octave_packages / m / statistics / base / run_count.m
1 ## Copyright (C) 1995-2012 Friedrich Leisch
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
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
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
16 ## along with Octave; see the file COPYING.  If not, see
17 ## <http://www.gnu.org/licenses/>.
18
19 ## -*- texinfo -*-
20 ## @deftypefn  {Function File} {} run_count (@var{x}, @var{n})
21 ## @deftypefnx {Function File} {} run_count (@var{x}, @var{n}, @var{dim})
22 ## Count the upward runs along the first non-singleton dimension of
23 ## @var{x} of length 1, 2, @dots{}, @var{n}-1 and greater than or equal
24 ## to @var{n}.
25 ##
26 ## If the optional argument @var{dim} is given then operate
27 ## along this dimension.
28 ## @end deftypefn
29
30 ## Author: FL <Friedrich.Leisch@ci.tuwien.ac.at>
31 ## Description: Count upward runs
32
33 function retval = run_count (x, n, dim)
34
35   if (nargin != 2 && nargin != 3)
36     print_usage ();
37   endif
38
39   if (! (isnumeric (x) || islogical (x)))
40     error ("run_count: X must be a numeric vector or matrix");
41   endif
42
43   if (!(isscalar (n) && n == fix (n) && n > 0))
44     error ("run_count: N must be a positive integer");
45   endif
46
47   nd = ndims (x);
48   sz = size (x);
49   if (nargin != 3)
50     ## Find the first non-singleton dimension.
51     (dim = find (sz > 1, 1)) || (dim = 1);
52   else
53     if (!(isscalar (dim) && dim == fix (dim))
54         || !(1 <= dim && dim <= nd))
55       error ("run_count: DIM must be an integer and a valid dimension");
56     endif
57   endif
58
59   ## Algorithm works on rows.  Permute array if necessary, ipermute back at end
60   if (dim != 1)
61     perm = [1 : nd];
62     perm(1) = dim;
63     perm(dim) = 1;
64     x = permute (x, perm);
65   endif
66
67   sz = size (x);
68   idx = cell ();
69   for i = 1 : nd
70     idx{i} = 1 : sz(i);
71   endfor
72   c = sz(1);
73   tmp = zeros ([c + 1, sz(2 : end)]);
74   infvec = Inf ([1, sz(2 : end)]);
75
76   ind = find (diff ([infvec; x; -infvec]) < 0);
77   tmp(ind(2:end) - 1) = diff (ind);
78   tmp = tmp(idx{:});
79
80   sz(1) = n;
81   retval = zeros (sz);
82   for k = 1 : (n-1)
83     idx{1} = k;
84     retval(idx{:}) = sum (tmp == k);
85   endfor
86   idx{1} = n;
87   retval(idx{:}) = sum (tmp >= n);
88
89   if (dim != 1)
90     retval = ipermute (retval, perm);
91   endif
92
93 endfunction
94
95
96 %!assert(run_count (magic(3), 4), [1,0,1;1,0,1;0,1,0;0,0,0])
97 %!assert(run_count (magic(3), 4, 2), [1,0,1;1,0,1;0,1,0;0,0,0]')
98 %!assert(run_count (5:-1:1, 5), [5, 0, 0, 0, 0])
99 %!assert(run_count (ones(3), 4), [0,0,0;0,0,0;1,1,1;0,0,0])
100
101 %% Test input validation
102 %!error run_count ()
103 %!error run_count (1)
104 %!error run_count (1, 2, 3, 4)
105 %!error run_count ({1, 2}, 3)
106 %!error run_count (['A'; 'A'; 'B'], 3)
107 %!error run_count (1:5, ones(2,2))
108 %!error run_count (1:5, 1.5)
109 %!error run_count (1:5, -2)
110 %!error run_count (1:5, 3, ones(2,2))
111 %!error run_count (1:5, 3, 1.5)
112 %!error run_count (1:5, 3, 0)
113