Finding last non-zero rows for individual columns of a matrix without loops
1 回表示 (過去 30 日間)
古いコメントを表示
I have the following code:
A =[1 2 3;
1 0 1;
0 5 1;
1 0 2;
2 3 2];
[row,~] = find(A(1:4,[1,2]),1,'last');
What I was hoping to achieve through the above code is to find the last non-zero rows for individual columns. So my expected answer was
row = [4 3] % since for the first column, the first non-zero element is on 4th row and similarly on 3rd row for the second column
However, with the above code, the answer I am getting is
row = 3
I have done it with a for loop, but with the size of data I have, it is computationally expensive to do loops. I was hoping to get it done without loops.
Thank you in advance for your kind contribution.
0 件のコメント
採用された回答
per isakson
2020 年 6 月 20 日
Try this
[r,c] = find( A==0 );
[~,ixa] = unique( c, 'last' );
>> r(ixa)
ans =
3
4
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Resizing and Reshaping Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!