Matrix Indices Problem
2 次查看(过去 30 天)
显示 更早的评论
Given a matrix e.g. A = [0 1 2 0 0; 0 3 4 5 0; 6 7 8 9 10; 0 0 0 11 0]
what MATLAB code will generate a vector of the column numbers of the first non-zero element in each row?
For this example the vector returned should be: [2;2;1;4]
0 个评论
采纳的回答
Sean de Wolski
2011-8-15
[junk, idx] = max(logical(A),[],2)
idx will be you index vector.
6 个评论
Fangjun Jiang
2011-8-15
Use sort().
A = [0 1 2 0 0; 0 3 4 5 0; 6 7 8 9 10; 0 0 0 11 0];
[dummy,Ind]=sort(A~=0,2)
Ind=Ind(:,end)
To find first non-zero,
A = [0 1 2 0 0; 0 3 4 5 0; 6 7 8 9 10; 0 0 0 11 0];
[dummy,Ind]=sort(A~=0,2,'descend')
Ind=Ind(:,1)
更多回答(1 个)
另请参阅
类别
在 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!