1######################################################################## 2## 3## Copyright (C) 1995-2021 The Octave Project Developers 4## 5## See the file COPYRIGHT.md in the top-level directory of this 6## distribution or <https://octave.org/copyright/>. 7## 8## This file is part of Octave. 9## 10## Octave is free software: you can redistribute it and/or modify it 11## under the terms of the GNU General Public License as published by 12## the Free Software Foundation, either version 3 of the License, or 13## (at your option) any later version. 14## 15## Octave is distributed in the hope that it will be useful, but 16## WITHOUT ANY WARRANTY; without even the implied warranty of 17## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18## GNU General Public License for more details. 19## 20## You should have received a copy of the GNU General Public License 21## along with Octave; see the file COPYING. If not, see 22## <https://www.gnu.org/licenses/>. 23## 24######################################################################## 25 26## -*- texinfo -*- 27## @deftypefn {} {} run_count (@var{x}, @var{n}) 28## @deftypefnx {} {} run_count (@var{x}, @var{n}, @var{dim}) 29## Count the upward runs along the first non-singleton dimension of @var{x} 30## of length 1, 2, @dots{}, @var{n}-1 and greater than or equal to @var{n}. 31## 32## If the optional argument @var{dim} is given then operate along this 33## dimension. 34## @seealso{runlength} 35## @end deftypefn 36 37function retval = run_count (x, n, dim) 38 39 if (nargin != 2 && nargin != 3) 40 print_usage (); 41 endif 42 43 if (! (isnumeric (x) || islogical (x))) 44 error ("run_count: X must be a numeric vector or matrix"); 45 endif 46 47 if (!(isscalar (n) && n == fix (n) && n > 0)) 48 error ("run_count: N must be a positive integer"); 49 endif 50 51 nd = ndims (x); 52 sz = size (x); 53 if (nargin != 3) 54 ## Find the first non-singleton dimension. 55 (dim = find (sz > 1, 1)) || (dim = 1); 56 else 57 if (!(isscalar (dim) && dim == fix (dim)) 58 || !(1 <= dim && dim <= nd)) 59 error ("run_count: DIM must be an integer and a valid dimension"); 60 endif 61 endif 62 63 ## Algorithm works on rows. Permute array if necessary, ipermute back at end 64 if (dim != 1) 65 perm = [1 : nd]; 66 perm(1) = dim; 67 perm(dim) = 1; 68 x = permute (x, perm); 69 endif 70 71 sz = size (x); 72 idx = cell (); 73 for i = 1 : nd 74 idx{i} = 1 : sz(i); 75 endfor 76 c = sz(1); 77 tmp = zeros ([c + 1, sz(2 : end)]); 78 infvec = Inf ([1, sz(2 : end)]); 79 80 ind = find (diff ([infvec; x; -infvec]) < 0); 81 tmp(ind(2:end) - 1) = diff (ind); 82 tmp = tmp(idx{:}); 83 84 sz(1) = n; 85 retval = zeros (sz); 86 for k = 1 : (n-1) 87 idx{1} = k; 88 retval(idx{:}) = sum (tmp == k); 89 endfor 90 idx{1} = n; 91 retval(idx{:}) = sum (tmp >= n); 92 93 if (dim != 1) 94 retval = ipermute (retval, perm); 95 endif 96 97endfunction 98 99 100%!assert (run_count (magic (3), 4), [1,0,1;1,0,1;0,1,0;0,0,0]) 101%!assert (run_count (magic (3), 4, 2), [1,0,1;1,0,1;0,1,0;0,0,0]') 102%!assert (run_count (5:-1:1, 5), [5, 0, 0, 0, 0]) 103%!assert (run_count (ones (3), 4), [0,0,0;0,0,0;1,1,1;0,0,0]) 104 105## Test input validation 106%!error run_count () 107%!error run_count (1) 108%!error run_count (1, 2, 3, 4) 109%!error run_count ({1, 2}, 3) 110%!error run_count (['A'; 'A'; 'B'], 3) 111%!error run_count (1:5, ones (2,2)) 112%!error run_count (1:5, 1.5) 113%!error run_count (1:5, -2) 114%!error run_count (1:5, 3, ones (2,2)) 115%!error run_count (1:5, 3, 1.5) 116%!error run_count (1:5, 3, 0) 117