1 function [counts0,counts1] = count_binary_runs(x)
3 if ~isvector(x) || ~islogical(x)
4 error('Input must be a logical vector.');
7 N_max = ceil(numel(x)/2);
8 counts0 = nan(N_max,1);
9 counts1 = nan(N_max,1);
13 x(end+1) = ~x(end); % Append one different bit to count correctly the last run
17 if x(i)~=current % new bit is flipped => append new run
20 counts1(idx_1) = i-idx;
23 counts0(idx_0) = i-idx;
30 counts0 = counts0(1:idx_0);
31 counts1 = counts1(1:idx_1);