フィルターのクリア

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

1 回表示 (過去 30 日間)
gummiyummi
gummiyummi 2020 年 8 月 3 日
コメント済み: gummiyummi 2020 年 8 月 3 日
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?

採用された回答

Stephen23
Stephen23 2020 年 8 月 3 日
>> 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 件のコメント
gummiyummi
gummiyummi 2020 年 8 月 3 日
thanks! works perfectly
gummiyummi
gummiyummi 2020 年 8 月 3 日
could you explain the annotation behind diff([0;V(:);0]) ?

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by