Count number of cells separating two trues in logical vector

1 Ansicht (letzte 30 Tage)
Hello!
I would like to find a fast procedure to count the number of cells separating two trues in a logical vector, and store the results in a new numeric vector.
For instance:
x=[true;false;false;false;true;false;false;true;false;true];
The output should be
output=[4;3;2]; % Hence, it counts also the "first" true
Hope the question is clear.
Thank you!

Akzeptierte Antwort

Matt J
Matt J am 12 Okt. 2022
x=[true;false;false;false;true;false;false;true;false;true];
diff(find(x))
ans = 3×1
4 3 2

Weitere Antworten (0)

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Produkte


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by