Finding the indexes of first zero value in rows of a matrix?

4 ビュー (過去 30 日間)
Marmar
Marmar 2017 年 11 月 27 日
コメント済み: Marmar 2017 年 11 月 28 日
Hi all, I have a matrix full of zero and one. However, I just want to have a vector including the index (Column number) of the first zero in each row. Any suggestion and help really appreciated.

採用された回答

Walter Roberson
Walter Roberson 2017 年 11 月 28 日
index_first_row_zero = sum( cumprod(logical(YourMatrix), 2), 2) + 1;
Note: in the case where none of the entries are 0, this returns one more than the width. You did not define the desired output for this case.
index_last_nonzero = size(YourMatrix,2) - sum( cumprod( ~logical(YourMatrix), 2, 'reverse'), 2);
Note: in the case where all of the entries are 0, this returns 0. You did not define the desired output for this case.
  1 件のコメント
Marmar
Marmar 2017 年 11 月 28 日
Yay! Thank you so much Walter! :)

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

その他の回答 (0 件)

カテゴリ

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