finding last non-zero value from column

1 回表示 (過去 30 日間)
Lizan
Lizan 2011 年 8 月 15 日
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
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
Lizan 2011 年 8 月 15 日
For example, I have a matrix M = (x,y,z).
I want to plot a line separating the 1-region with the 0-region for each z.
for example
z1 = d1; M1 = [ 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 ];
z2 = d2; M2 = [ 1 1 1 1 1 1 ; 1 1 1 1 1 1 ; 1 1 1 1 1 0 ; 1 1 1 1 0 0; 1 1 1 0 0 0 ];
z3 = d3; M3 = [ 1 1 1 1 1 1 ; 1 1 1 1 1 0 ; 1 1 1 1 1 0 ; 1 1 1 1 0 0; 1 1 1 1 0 0 ];
z4 = d4; M4 = [ 1 0 0 0 0 0 ; 1 0 0 0 0 0 ; 1 1 0 0 0 0 ; 1 1 0 0 0 0; 1 1 0 0 0 0 ];
where d1,d2,d3,d4 is some parameter.

サインインしてコメントする。

採用された回答

Fangjun Jiang
Fangjun Jiang 2011 年 8 月 15 日
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 ];
n=M~=0;
[dummy,Index]=sort(n);
Index=Index(end,:).*any(n)
Use M=randint(x,y) to generate testing data, I am thinking my solution has an edge.
for 3D matrix:
clear M;clc;
M(1,:,:) = [ 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 ];
M(2,:,:) = [ 1 1 1 1 1 1 ; 1 1 1 1 1 1 ; 1 1 1 1 1 0 ; 1 1 1 1 0 0; 1 1 1 0 0 0 ];
M(3,:,:) = [ 1 1 1 1 1 1 ; 1 1 1 1 1 0 ; 1 1 1 1 1 0 ; 1 1 1 1 0 0; 1 1 1 1 0 0 ];
M(4,:,:) = [ 1 0 0 0 0 0 ; 1 0 0 0 0 0 ; 1 1 0 0 0 0 ; 1 1 0 0 0 0; 1 1 0 0 0 0 ];
n=M~=0;
[dummy,Index]=sort(n,2);
Index=Index(:,end,:).*any(n,2);
Index=reshape(Index,size(M,1),size(M,3))
  3 件のコメント
Fangjun Jiang
Fangjun Jiang 2011 年 8 月 15 日
Yeah. I need to work on the 3D Matrix. My brain is flat now!
Fangjun Jiang
Fangjun Jiang 2011 年 8 月 15 日
All right! See updated version! Considered all-zero columns for both 2D and 3D!

サインインしてコメントする。

その他の回答 (2 件)

Andrei Bobrov
Andrei Bobrov 2011 年 8 月 15 日
in your case
sum(M)
ADD
sum(cumsum(flipud(M~=0))~=0)
  3 件のコメント
Lizan
Lizan 2011 年 8 月 15 日
That is effective.. how would this work for matrix like M4 (which the zeros is on the top instead of below)?
Fangjun Jiang
Fangjun Jiang 2011 年 8 月 15 日
sum() won't work for cases like [0 0;1 1]

サインインしてコメントする。


Sean de Wolski
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

カテゴリ

Help Center および File ExchangeData Distribution Plots についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by