How find non zero

1 回表示 (過去 30 日間)
So
So 2016 年 6 月 6 日
コメント済み: So 2016 年 6 月 6 日
Hello,
For example I have a vector A = [ 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0]. And I do find(A,2)-2, so I have the 2 zeros before the first one. However, how can I get the 5 zeros before the first one ? If i do find(A,5)-5 I will get only the 4 zeros because there is only four one.
Thank you.

採用された回答

Stephen23
Stephen23 2016 年 6 月 6 日
>> A = [ 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0];
>> fun = @(n)find(A,1)-(n:-1:1);
>> fun(2)
ans =
6 7
>> fun(5)
ans =
3 4 5 6 7
  1 件のコメント
So
So 2016 年 6 月 6 日
Thank you Stephen :)

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2016 年 6 月 6 日
strfind(A, [0 0 0 0 0 1])

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by