identify consecutive occurrence of 1 in a binary array
21 views (last 30 days)
Show older comments
A = [0,0,0,0,1,1,0,0 ...
1,0,1,1,1,1,0,0 ...
0,1,1,0,1,0,0,1]'
Given A , I want to determine the 1 consecutives count, 2 consecutives count , up to N = length(A) for '1' in a binary array.
When I count the consecutive numbers, I consider index [i , i+1].
For example:
count1 = 11
count2 = 5
count3 = 2
count4 = 1
count5 = 0
countN = 0.
3 Comments
Answers (3)
Voss
on 4 Mar 2022
You can use strfind() to find runs of consecutive 1's:
A = [0,0,0,0,1,1,0,0 ...
1,0,1,1,1,1,0,0 ...
0,1,1,0,1,0,0,1]';
counts = zeros(1,numel(A));
for ii = 1:numel(A)
counts(ii) = numel(strfind(A(:).',ones(1,ii)));
end
disp(counts)
0 Comments
Image Analyst
on 4 Mar 2022
If you have the Image Processing Toolbox, you can do it in two lines of code:
% Define column vector
A = [0,0,0,0,1,1,0,0 ...
1,0,1,1,1,1,0,0 ...
0,1,1,0,1,0,0,1]'
% Measure lengths of all runs of 1's:
props = regionprops(logical(A), 'Area');
% Take histogram to get distribution of the run lengths.
counts = histcounts([props.Area])
3 Comments
Image Analyst
on 4 Mar 2022
Why should it be 11? You do not have 11 occurrences of single 1's, meaning a single one surrounded by 0 on both sides. You have only 3 single isolated 1's in the sample A vector you posted.
Walter Roberson
on 4 Mar 2022
The question appears to be "how many times is there at least one consecutive 1, including each sub-occurence ? How many times is there at least two consecutive 1, including each sub-occurence?" and so on.
A = [0,0,0,0,1,1,0,0 ...
1,0,1,1,1,1,0,0 ...
0,1,1,0,1,0,0,1]
length(strfind(A, ones(1,1)))
length(strfind(A, ones(1,2)))
length(strfind(A, ones(1,3)))
length(strfind(A, ones(1,4)))
length(strfind(A, ones(1,5)))
Jan
on 4 Mar 2022
Edited: Jan
on 4 Mar 2022
A = [0,0,0,0,1,1,0,0, ... % Using a row vector instead
1,0,1,1,1,1,0,0, ...
0,1,1,0,1,0,0,1];
% Find longest run:
[b, n] = RunLengthEnc(A);
n = n(b == 1); % Care for runs of 1's only
m = max(n);
counts = zeros(1, numel(A));
for ii = 1:m
% Number of times a block of ii ones matchs into blocks of n ones:
k = n - ii + 1;
counts(ii) = sum(max(k, 0)); % Count positive elements only
end
disp(counts)
function [b, n] = RunLengthEnc(x)
d = [true, diff(x) ~= 0]; % TRUE if values change
b = x(d); % Elements without repetitions
n = diff(find([d, true])); % Number of repetitions
end
Searching for all lengths of blocks is very expensive, because the computing time grows exponentially. So determine the longest block of ones at first. But if you do have the lengths of the existing blocks, it is cheap to use this directly instead of letting strfind search again.
1 Comment
Jan
on 4 Mar 2022
Edited: Jan
on 4 Mar 2022
A speed comparison:
A = randi([0,1], 1, 1e5); % Test data
tic; % CONV: ----------------------------------------------------
[b, n] = RunLengthEnc(A); % Determine longest run
m = max(n(b == 1));
counts1 = zeros(1, numel(A));
for ii = 1:m
counts1(ii) = nnz(conv(A, ones(1, ii), 'same') == ii);
end
toc % Half speed of NUMEL(STRFIND):
tic; % NUMEL(STRFIND): -----------------------------------------
counts2 = zeros(1,numel(A));
for ii = 1:numel(A)
counts2(ii) = numel(strfind(A, ones(1,ii)));
end
toc % Very slow to search for all lengths - growing exponentially:
tic % NUMEL(STRFIND) but limited to longest run: ----------------
[b, n] = RunLengthEnc(A);
m = max(n(b == 1));
counts3 = zeros(1,numel(A));
for ii = 1:m % With a fair limit
counts3(ii) = numel(strfind(A, ones(1,ii)));
end
toc % Much better to limit search to exitsing blocks:
tic % Use the calculated run lengths directly: ------------------
[b, n] = RunLengthEnc(A);
n = n(b == 1);
m = max(n);
counts4 = zeros(1, numel(A));
for ii = 1:m
k = n - ii + 1;
counts4(ii) = sum(sum(max(k, 0)));
end
toc % Twice as fast as using STRFIND repeatedly
isequal(counts1, counts2, counts3, counts4) % Same result
function [b, n] = RunLengthEnc(x)
% See also: https://www.mathworks.com/matlabcentral/fileexchange/41813-runlength
d = [true; diff(x(:)) ~= 0]; % TRUE if values change
b = x(d); % Elements without repetitions
n = diff(find([d', true])); % Number of repetitions
end
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!