finding last non-zero value from column
古いコメントを表示
Given a matrix looking something like e.g. M = [ 1 1 1 1 1 1 ; 1 1 1 0 0 0 ; 1 1 0 0 0 0 ; 1 0 0 0 0 0; 0 0 0 0 0 0 ];
How can I find the coordinate (for plotting these values as a line) for which states the last non-zero element in each column?
For this example I want the coordinates for the column number [ 4, 3, 2, 1, 1, 1]
Would the same code work for 3D-matrix?
2 件のコメント
Sean de Wolski
2011 年 8 月 15 日
We could absolutely get the code to work for a 3d matrix, but you have to define what you want. Would you want a two d plane through the third dimension with each column's contribution, or would you like it reshaped?
Lizan
2011 年 8 月 15 日
採用された回答
その他の回答 (2 件)
Andrei Bobrov
2011 年 8 月 15 日
in your case
sum(M)
ADD
sum(cumsum(flipud(M~=0))~=0)
3 件のコメント
Sean de Wolski
2011 年 8 月 15 日
Nice, no reason for the ~=0 tests
sum(logical(cumsum(flipud(M))))
Lizan
2011 年 8 月 15 日
Fangjun Jiang
2011 年 8 月 15 日
sum() won't work for cases like [0 0;1 1]
Sean de Wolski
2011 年 8 月 15 日
[junk, idx] = max(flipud(M),[],1); %flip it and find first maximizer
idx = size(M,1)-idx+1
for 3d:
[junk, idx] = max(flipdim(rand(10,10,10)>.5,1),[],1);
idx = size(M,1)-idx+1
カテゴリ
ヘルプ センター および File Exchange で Surface and Mesh Plots についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!