Matrix Indices Problem
古いコメントを表示
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]
採用された回答
その他の回答 (1 件)
Andrei Bobrov
2011 年 8 月 15 日
size(A,2)+1-sum(cumsum(A,2)~=0,2)
more
(sum(cumsum(A')==0)+1)'
2 件のコメント
mutt
2011 年 8 月 15 日
Andrei Bobrov
2011 年 8 月 16 日
Hi Mutt! my variant:
sum(cumsum(flipud(A'))>0)'
カテゴリ
ヘルプ センター および File Exchange で Matrix Indexing についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!