Help counting a string of 1s in 2018a?

2 views (last 30 days)
I want to write a function that will detect and count the number of strings of consecutive 1's in a vector.
For example, if I have a vector of [01110011100], I am looking for a function that will give me an answer of 2, as there are 2 strings of consequetive 1s

Accepted Answer

Raj
Raj on 28 May 2019
You can use something like this:
A=[0 1 1 1 0 0 1 1 1 0 0]
A1 = [1,diff(A)]~=0;
Count = [A(A1)'];
Required_Answer=sum(Count(:) == 1)

More Answers (0)

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!