Calculate true in binary array specifically

1 回表示 (過去 30 日間)
Ivan Volodin
Ivan Volodin 2018 年 9 月 23 日
コメント済み: Ivan Volodin 2018 年 9 月 23 日
Hello!
I have a boolean one-dimensional array, like
binary = [0,0,0,1,0,1,0,1,1,1,0,1,1,1,0,0,0,1,1,0 ..., 0]
And I like to squeeze from this the following:
[1, 1, 3, 3, 2, ...]
So, I would like to calculate how many 1s (Trues) encounter in row. It is possible to write a loop somehow, like:
for i=1:size(array)
if B(14:i)== 1
count = count + 1;
else:
count = 0;
Result_Array.append(count) % I do not know builtin function exaclty.
end
Is it correct..? And I would like to know, if there is a standart matlab function for this purpose.
Thank you very much!

採用された回答

Stephen23
Stephen23 2018 年 9 月 23 日
編集済み: Stephen23 2018 年 9 月 23 日
This is MATLAB, so you don't need to use loops to solve every little task:
>> V = [0,0,0,1,0,1,0,1,1,1,0,1,1,1,0,0,0,1,1,0,0];
>> D = diff([0,V,0]);
>> find(D<0)-find(D>0)
ans =
1 1 3 3 2
  1 件のコメント
Ivan Volodin
Ivan Volodin 2018 年 9 月 23 日
Thank you! I just do not feel this language.. Not yet

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

その他の回答 (0 件)

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by