フィルターのクリア

How can I find consecutive ones and the number of ones in each block?

15 ビュー (過去 30 日間)
Tasi
Tasi 2015 年 3 月 4 日
I have an example variable defined as x=[5 1 1 1 1 1 7 1 1 5]; I would like to get the number of ones in each block of consecutive ones, so that my output would look like this: [5 2]. Is there a way to do write a command so that is valid in a loop in which the variable changes both its values and length? Thanks.

採用された回答

John D'Errico
John D'Errico 2015 年 3 月 4 日
x=[5 1 1 1 1 1 7 1 1 5];
start1 = strfind([0,x==1],[0 1]);
end1 = strfind([x==1,0],[1 0]);
end1 - start1 + 1
ans =
5 2
  2 件のコメント
Tasi
Tasi 2015 年 3 月 4 日
Thanks John, I appreciate it!
Praveen Kumar Pakkirisamy
Praveen Kumar Pakkirisamy 2020 年 9 月 4 日
That's clever. THANKS

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeModeling についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by