How to find the column index of the first nonzero element for every row of a given matrix
1 次查看(过去 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]
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]
How can I do this? Thanks in advance, I appreciate your time and help.
2 个评论
采纳的回答
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)
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!