Count number of cells separating two trues in logical vector

조회 수: 1 (최근 30일)
Enrico Gambini
Enrico Gambini 2022년 10월 12일
댓글: Enrico Gambini 2022년 10월 12일
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!

채택된 답변

Matt J
Matt J 2022년 10월 12일
x=[true;false;false;false;true;false;false;true;false;true];
diff(find(x))
ans = 3×1
4 3 2

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by