Help counting a string of 1s in 2018a?

2 vues (au cours des 30 derniers jours)
friendlylouie
friendlylouie le 27 Mai 2019
Commenté : friendlylouie le 30 Mai 2019
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

Réponse acceptée

Raj
Raj le 28 Mai 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)
  1 commentaire
friendlylouie
friendlylouie le 30 Mai 2019
Thanks very much!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by