how to count array of 1 and 0 repeated times

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.

Antworten (3)

David Hill
David Hill am 17 Mär. 2022

0 Stimmen

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 am 17 Mär. 2022
Bearbeitet: Matt J am 17 Mär. 2022

0 Stimmen

Using,
[~,~,runlengths]=groupLims(groupTrue(data==1),1);
count= nnz(runlengths>=10)

18 Kommentare

Keaton Looper
Keaton Looper am 18 Mär. 2022
some of the functions don't work
Matt J
Matt J am 18 Mär. 2022
Bearbeitet: Matt J am 18 Mär. 2022
Possibly because you didn't download them? Here's an example,
e=ones(1,10);
data=[e,0,0,e 1 0 0 0 1 1];
[~,~,runlengths]=groupLims(groupTrue(data==1),1);
count= nnz(runlengths>=10)
count = 2
Keaton Looper
Keaton Looper am 18 Mär. 2022
i already have a data set titled diff that contains set of ones and zeros
Matt J
Matt J am 18 Mär. 2022
Yes, but I don't. That's why I needed to make up my own data for the example.
so then would i just put
data = diff
[~,~,runlengths]=groupLims(groupTrue(data==1),1);
count= nnz(runlengths>=10)
so that it assigns my data set to the variable data?
Yes, or you could just do as below, although I would advise you not to name your variable 'diff' since it conflicts with an existing Matlab function.
[~,~,runlengths]=groupLims(groupTrue(diff==1),1);
count= nnz(runlengths>=10)
Keaton Looper
Keaton Looper am 18 Mär. 2022
what would i do if i wanted to add another condition to the count. Lets say if the the number one is repeated 10 times consecutively and if the respective visibility value is less than 2 miles?
Matt J
Matt J am 18 Mär. 2022
and if the respective visibility value is less than 2 miles
I don't think you've told us what this is.
Keaton Looper
Keaton Looper am 18 Mär. 2022
So I have two conditions I need the counter to meet. When 1 is repeated consecutively 10 times and if visibility value from another data set is less than two miles. So I need it to take into account two conditions it needs to meet to count the event as a 1.
Matt J
Matt J am 18 Mär. 2022
Bearbeitet: Matt J am 18 Mär. 2022
Is the "visibility value" data set a vector the same size of "diff"? So <2 miles means the visibility vector elements are <2 ten times consecutively as well?
Keaton Looper
Keaton Looper am 18 Mär. 2022
Yes the visibility vector is a different data set.
Matt J
Matt J am 18 Mär. 2022
Bearbeitet: Matt J am 18 Mär. 2022
Is the "visibility value" data set a vector the same size as the first data set? So <2 miles means the visibility vector elements are <2 ten times consecutively as well?
Keaton Looper
Keaton Looper am 18 Mär. 2022
Yes
Keaton Looper
Keaton Looper am 18 Mär. 2022
Both the vectors at the same length. The logically vector is the same length until the zeros are not included then it shortens
[~,~,runlengths]=groupLims(groupTrue(data==1 & visibility<2),1);
count= nnz(runlengths>=10)
Image Analyst
Image Analyst am 18 Mär. 2022
"some of the functions don't work " <== if you don't want to use a File Exchange program and want to use a built-in function that will do it in a single line of code, see my answer below.
Keaton Looper
Keaton Looper am 18 Mär. 2022
its giving me a count of zero when i know it should not be
Image Analyst
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.

Melden Sie sich an, um zu kommentieren.

Image Analyst
Image Analyst am 18 Mär. 2022
Bearbeitet: Image Analyst am 18 Mär. 2022

0 Stimmen

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

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

Melden Sie sich an, um zu kommentieren.

Kategorien

Gefragt:

am 17 Mär. 2022

Kommentiert:

am 18 Mär. 2022

Community Treasure Hunt

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

Start Hunting!

Translated by