필터 지우기
필터 지우기

Shifting elements to the left / top

조회 수: 2 (최근 30일)
Jeon
Jeon 2013년 4월 19일
a = [1 2 3;
NaN 4 5;
6 NaN 7;
8 9 NaN;
NaN NaN 10;
NaN 11 NaN;
12 NaN NaN;
NaN NaN NaN]
I'd like to transform a matrix above into the following:
a = [1 2 3;
4 5 NaN;
6 7 NaN;
8 9 NaN;
10 NaN NaN;
11 NaN NaN;
12 NaN NaN;]
i.e. remove all NaN elements and shift remaining elements to the left. And if dimension mismatch, fill with NaN into it.
No iterations preferred

답변 (1개)

Matt J
Matt J 2013년 4월 19일
b=a.';
bmap0=isnan(b);
b(:,all(bmap0,1))=[];
bmap0=~isnan(b);
data=b(bmap0);
b(~bmap0)=inf;
b=sort(b);
bmap1=~isinf(b);
b(bmap1)=data(:);
bmap2=isinf(b);
b(isinf(b))=nan;
a=b.'
  댓글 수: 1
Matt J
Matt J 2013년 4월 19일
A lot of this would simplify if you worked columnwise, instead of row-wise and used Infs instead of NaNs

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by