Count number of repeated element before next different number in array?
1 回表示 (過去 30 日間)
古いコメントを表示
Hi,
This is the problem:
I have an array of size 1xN, and this array is filled with 1 and 0. What I want is to count the number of repeated ones before a zero.
I will try to explain with an example.
A = [1,1,1,0,0,0,0,1,0,0,1,1,0,0,0,1,1,1,1];
What I want the code to do is to count how many times the ones are repeated before a zero. So, the answer is
OnesRepeated = [3, 1, 2, 4]
Can anyone help please? Thanks
0 件のコメント
採用された回答
その他の回答 (1 件)
Adam
2019 年 1 月 14 日
runs = diff( [0 find( diff( A ) ) numel( A )] )
if A(1) == 1
OnesRepeated = runs( 1:2:end );
else
OnesRepeated = runs( 2:2:end );
end
1 件のコメント
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!