how to count array of 1 and 0 repeated times

조회 수: 6 (최근 30일)
Keaton Looper
Keaton Looper 2022년 3월 17일
댓글: Image Analyst 2022년 3월 18일
I am tryig to make a counter that will look at a set of data that consists of ones and zeros and count how many times the number 1 is repeated 10 times consecutively and count that occurence as a 1 and any other occurence of a zero or less than 10 times as a zero.

답변 (3개)

David Hill
David Hill 2022년 3월 17일
a=ones(1,10);
b=[];
for k=1:10
b=[b,randi(2,1,20)-1,a];
end
% above just generates binary series
s=num2str(b);
s=s(s~=' ');
count=length(regexp(s,'[1]{10,}'));

Matt J
Matt J 2022년 3월 17일
편집: Matt J 2022년 3월 17일
  댓글 수: 18
Keaton Looper
Keaton Looper 2022년 3월 18일
its giving me a count of zero when i know it should not be
Image Analyst
Image Analyst 2022년 3월 18일
I gave an example below where I prove it worked. If it doesn't work for your vector, attach your vector in a text or mat file.

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


Image Analyst
Image Analyst 2022년 3월 18일
편집: Image Analyst 2022년 3월 18일
If you have the Image Processing Toolbox, you can use the built-in bwlabel and do it in one line of code. If m is your matrix:
[~, count] = bwlabel(bwareafilt(logical(m), [10, inf]))
  댓글 수: 1
Image Analyst
Image Analyst 2022년 3월 18일
Here is an example
m = zeros(1, 1000);
% Make 3 stretches of 10 or more:
m(100:110) = 1;
m(330:380) = 1;
m(560:590) = 1;
[~, count] = bwlabel(bwareafilt(logical(m), [10, inf]))
count = 3

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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by