how to count array of 1 and 0 repeated times
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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.
0 Kommentare
Antworten (3)
David Hill
am 17 Mär. 2022
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,}'));
0 Kommentare
Matt J
am 17 Mär. 2022
Bearbeitet: Matt J
am 17 Mär. 2022
Using,
[~,~,runlengths]=groupLims(groupTrue(data==1),1);
count= nnz(runlengths>=10)
18 Kommentare
Image Analyst
am 18 Mär. 2022
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
am 18 Mär. 2022
Bearbeitet: Image Analyst
am 18 Mär. 2022
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 Kommentar
Image Analyst
am 18 Mär. 2022
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]))
Siehe auch
Kategorien
Mehr zu Cell Arrays finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!