How to find the column index of the first nonzero element for every row of a given matrix

3 ビュー (過去 30 日間)
Dear all,
I have the following matrix
A=[1 1 1 1; 0 1 1 1; 0 0 0 1; 0 0 1 1; 0 1 1 1]
A = 5×4
1 1 1 1 0 1 1 1 0 0 0 1 0 0 1 1 0 1 1 1
and I would like to get an output vector v listing the column indices of the first nonzero element for every row of A:
v=[1;2;4;3;2]
v = 5×1
1 2 4 3 2
How can I do this? Thanks in advance, I appreciate your time and help.
  2 件のコメント
Distelfink
Distelfink 2022 年 3 月 6 日
Not at all, but this was a different problem I thought. Matt's solution in that thread, to use the find() function, did not work for me in the case here because it yields the column indices of all non-negative values in each row.

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

採用された回答

Arif Hoq
Arif Hoq 2022 年 3 月 6 日
you can find your answer here
Just follow this:
A=[1 1 1 1; 0 1 1 1; 0 0 0 1; 0 0 1 1; 0 1 1 1];
[~,output]=max(A,[],2)
output = 5×1
1 2 4 3 2

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by