Count number of indexes for each consecutive values in column of array

1 vue (au cours des 30 derniers jours)
gummiyummi
gummiyummi le 3 Août 2020
Commenté : gummiyummi le 3 Août 2020
I have an array of a variable of dimension: 23999 x 1
The array consists of 1s and 0s.
I want to count how many indexes each consecutive 1s take up. For example:
[0 0 1 1 1 0 0 0 0 0 1 1 1 1 1 1 1 1 1 0 0 0] (vertically of course)
the output I would like is 3 9
I tried using diff and find but keep getting the error horzcat... Can somebody help me?

Réponse acceptée

Stephen23
Stephen23 le 3 Août 2020
>> V = [0 0 1 1 1 0 0 0 0 0 1 1 1 1 1 1 1 1 1 0 0 0];
>> D = diff([0;V(:);0]);
>> find(D<0)-find(D>0)
ans =
3
9
  2 commentaires
gummiyummi
gummiyummi le 3 Août 2020
thanks! works perfectly
gummiyummi
gummiyummi le 3 Août 2020
could you explain the annotation behind diff([0;V(:);0]) ?

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Multidimensional Arrays 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