How to extract first non-zero element in each column and put into a new array

12 ビュー (過去 30 日間)
Kyle Vanlerberghe
Kyle Vanlerberghe 2019 年 9 月 16 日
回答済み: Andrei Bobrov 2019 年 9 月 16 日
I have an array which represents a 2-D vertical slice of a cloud. I want to get cloud-top properties for the cloud so I want to just plot a line graph of the top layer. But the row corresponding to the first non-zero number changes with each column. Does anyone know how I can extract the first non-zero element in each column and put that into a new array? For example:
in=[0 0 6 0 0 5;
1 3 5 1 0 4;
4 4 5 3 4 5]
out=[1 3 6 1 4 5]

回答 (4 件)

Andrei Bobrov
Andrei Bobrov 2019 年 9 月 16 日
out = in(cumsum(cumsum(in~=0)) == 1)'

the cyclist
the cyclist 2019 年 9 月 16 日
編集済み: the cyclist 2019 年 9 月 16 日
Here is a one way:
in=[0 0 6 0 0 5;
1 3 5 1 0 4;
4 4 5 3 4 5];
[m,n] = size(in);
[i,j] = find(in);
[~,jj] = unique(j);
out = in(i(jj)+(0:m:(m*(n-1)))')'

the cyclist
the cyclist 2019 年 9 月 16 日
編集済み: the cyclist 2019 年 9 月 16 日
Here is one way:
in=[0 0 6 0 0 5;
1 3 5 1 0 4;
4 4 5 3 4 5];
mid = in;
for nr = size(in,1)-1:-1:1
mid(nr,mid(nr,:)==0) = mid(nr+1,mid(nr,:)==0);
end
out = mid(1,:);

the cyclist
the cyclist 2019 年 9 月 16 日
Here is one way:
in=[0 0 6 0 0 5;
1 3 5 1 0 4;
4 4 5 3 4 5];
[m,n] = size(in);
out = nan(1,n);
for nc = 1:n
[~,~,out(nc)] = find(in(:,nc),1);
end

カテゴリ

Help Center および File ExchangePoint Cloud Processing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by