identify consecutive occurrence of 1 in a binary array

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]'
A = 24×1
0 0 0 0 1 1 0 0 1 0
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

Does the matrix shape of the input matter? If not, why not posting a vector as test data? This would reduce the confusion, if the elements should be processes row- or column-wise.
Why is "Count1" 11 and not 3? Waht is "N" in "CountN"? Why is Count3 2?
A is a column vector.
Count1 is total number of "1" in the vector.
i want to find the count of where "1" occurs as pairs , triplet etc, In this eg, there are 5 pairs of 1 at index(A) = (5,6); (11,12); (12,13); (13,14); (18,19) hence count2 = 5;
similiarly, count3 is when 3 "1" occur simultaneously at index(A) = (11,12,13);(12,13,14)

Sign in to comment.

Answers (3)

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)
11 5 2 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
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]'
A = 24×1
0 0 0 0 1 1 0 0 1 0
% 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])
counts = 1×4
3 2 0 1

3 Comments

can you explain me the why counts is 3 2 0 1 and not 11, 5, 2, 1, 0
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.
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]
A = 1×24
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)))
ans = 11
length(strfind(A, ones(1,2)))
ans = 5
length(strfind(A, ones(1,3)))
ans = 2
length(strfind(A, ones(1,4)))
ans = 1
length(strfind(A, ones(1,5)))
ans = 0

Sign in to comment.

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)
11 5 2 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
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

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):
Elapsed time is 0.039254 seconds.
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:
Elapsed time is 34.704360 seconds.
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:
Elapsed time is 0.018021 seconds.
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
Elapsed time is 0.008824 seconds.
isequal(counts1, counts2, counts3, counts4) % Same result
ans = logical
1
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

Sign in to comment.

Categories

Products

Release

R2021b

Asked:

on 4 Mar 2022

Edited:

Jan
on 4 Mar 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!