フィルターのクリア

Find the first incidence of 1 in a logical vector and replace all other 1s by zero

2 ビュー (過去 30 日間)
susman
susman 2020 年 7 月 23 日
コメント済み: susman 2020 年 7 月 23 日
I need to find the first incidence of 1 in a logical vector and replace all other 1s by zero.
For example, I have a logical vector like this: V = [ 0 1 0 0 1 0 1 ] and I need a function/code that will tell me that will yield V=[0 1 0 0 0 0 0]..
Since I am having a matrix (10000,35) size, how to run this code for each row?

採用された回答

madhan ravi
madhan ravi 2020 年 7 月 23 日
Wanted = (cumsum(matrix, 2) == 1) .* matrix
  2 件のコメント
Walter Roberson
Walter Roberson 2020 年 7 月 23 日
This answer is better than mine.
susman
susman 2020 年 7 月 23 日
Thank you so much! I appreciate your help :)

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2020 年 7 月 23 日
diff([zeros(size(YourMatrix,1),1),~cumprod(~YourMatrix,2)],[],2)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by