identify consecutive occurrence of 1 in a binary array

조회 수: 8 (최근 30일)
pallavi patil
pallavi patil 2022년 3월 4일
편집: Jan 2022년 3월 4일
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
pallavi patil
pallavi patil 2022년 3월 4일
A is a column vector.
Count1 is total number of "1" in the vector.
pallavi patil
pallavi patil 2022년 3월 4일
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)

댓글을 달려면 로그인하십시오.

답변 (3개)

Voss
Voss 2022년 3월 4일
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

Image Analyst
Image Analyst 2022년 3월 4일
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
Image Analyst
Image Analyst 2022년 3월 4일
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
Walter Roberson 2022년 3월 4일
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

댓글을 달려면 로그인하십시오.


Jan
Jan 2022년 3월 4일
편집: Jan 2022년 3월 4일
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
Jan
Jan 2022년 3월 4일
편집: Jan 2022년 3월 4일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by