Hi all,
I have a vector consisting of booleans, either 1 or 0. Such as {0 0 0 0 1 1 1 0 0 0 0 0 1 1 0 0 0 0}. 1 values by itself cannot be in between of 0s. The same rule is valid also for 0s. I need the length of 1s separately. In other words, I do not want to know the length of all 1s. Instead, what I look for is the length of each 1s accumulated inside the vector independently. How can I find the length of 1s via matlab?
Thank you in advence.

 採用された回答

Adam Danz
Adam Danz 2019 年 4 月 16 日
編集済み: Adam Danz 2019 年 4 月 16 日

0 投票

The vector 'oneCounts' is a list of consecutive 1 counts. It's empty if there are no 1s.
tf = [0 0 0 0 0 1 1 1 0 0 0 0 0 1 1 0 0 0 0 1 1 1 0 0 1 1 1 1];
df = diff([0, tf, 0]); %if tf is a column: diff([0;tf;0])
oneCounts = find(df == -1) - find(df == 1)
oneCounts =
3 2 3 4

2 件のコメント

Oguz Altunkas
Oguz Altunkas 2019 年 4 月 16 日
Thank you so much, it helped alot.
Kind Regards
Adam Danz
Adam Danz 2019 年 4 月 16 日
編集済み: Adam Danz 2019 年 4 月 17 日
Glad I could help!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLanguage Fundamentals についてさらに検索

質問済み:

2019 年 4 月 16 日

編集済み:

2019 年 4 月 17 日

Community Treasure Hunt

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

Start Hunting!

Translated by