replacing NaN with it previous element

조회 수: 1 (최근 30일)
antonet
antonet 2012년 8월 8일
Dear all,
I have
K={
'IT' 'ooil' 'TTT' [ 0] [4.1583]
'IT' 'ooil' 'TTT' [ 0] [4.0339]
'IT' 'ooil' 'TTT' [ 0] [3.9389]
'IT' 'ooil' 'TTT' [ 0] [3.9676]
'ITn' 'ooilb' 'TTT' [ 0] [3.9313]
[NaN] [ NaN] [NaN] [ 0] [3.9313]
'IT' 'ooil' 'TTT' [ 0] [3.8720]
'ITcv' 'ooila' 'TTT' [ 0] [3.8829]
[NaN] [ NaN] [NaN] [ 0] [3.9313]
'IT' 'ooil' 'TTT' [ 0] [3.8681]
'ITf' 'ooilg' 'TTT' [ 0] [4.3944]
[NaN] [ NaN] [NaN] [ 0] [3.9313]}
And I want to replace the NaNs in each column with its previous element of the same column
That is,
K={
'IT' 'ooil' 'TTT' [ 0] [4.1583]
'IT' 'ooil' 'TTT' [ 0] [4.0339]
'IT' 'ooil' 'TTT' [ 0] [3.9389]
'IT' 'ooil' 'TTT' [ 0] [3.9676]
'ITn' 'ooilb' 'TTT' [ 0] [3.9313]
'ITn' 'ooilb' 'TTT' [ 0] [3.9313]
'IT' 'ooil' 'TTT' [ 0] [3.8720]
'ITcv' 'ooila' 'TTT' [ 0] [3.8829]
'ITcv' 'ooila' 'TTT' [ 0] [3.9313]
'IT' 'ooil' 'TTT' [ 0] [3.8681]
'ITf' 'ooilg' 'TTT' [ 0] [4.3944]
'ITf' 'ooilg' 'TTT' [ 0] [3.9313]}
Thanks in advance

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2012년 8월 8일
res=K,
for k=1:size(K,1);
if isnan(K{k,1});
res(k,1:3)=K(k-1,1:3);
end;
end

추가 답변 (2개)

Isktaine
Isktaine 2012년 8월 8일
편집: Isktaine 2012년 8월 8일
Save this function then use it on K:
function K = RemoveNaNs(K)
[rows,cols]=size(K); %Works out how many rows and columns there are
for p=1:rows
for q=1:cols
if isnan(K{p,q})==1 %If there is a NaN entry
K{p,q}=K{p-1,q}; %Replace it with the entry from the previous row.
end
end
end
end
Is this clear?
  댓글 수: 3
John Petersen
John Petersen 2012년 8월 8일
That wouldn't be a problem because you've already replaced the 1st nan. The only problem would be a nan in the very 1st line. Then p-1=0 and you would get an error. So p needs to start at 2 and there needs to be an initialization in case the first row has a nan.
Isktaine
Isktaine 2012년 8월 8일
Yeah John is right. I didn't know what antonet would want the value to be if there was a NaN in the first row, so I didn't address this.
Thanks Lucas! :)

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


Thomas
Thomas 2012년 8월 8일
K={
'IT' 'ooil' 'TTT' [ 0] [4.1583]
'IT' 'ooil' 'TTT' [ 0] [4.0339]
'IT' 'ooil' 'TTT' [ 0] [3.9389]
'IT' 'ooil' 'TTT' [ 0] [3.9676]
'ITn' 'ooilb' 'TTT' [ 0] [3.9313]
[NaN] [ NaN] [NaN] [ 0] [3.9313]
'IT' 'ooil' 'TTT' [ 0] [3.8720]
'ITcv' 'ooila' 'TTT' [ 0] [3.8829]
[NaN] [ NaN] [NaN] [ 0] [3.9313]
'IT' 'ooil' 'TTT' [ 0] [3.8681]
'ITf' 'ooilg' 'TTT' [ 0] [4.3944]
[NaN] [ NaN] [NaN] [ 0] [3.9313]}
% needs only 3 lines of code
A=K;
[r,c]=find(cellfun(@(V) any(isnan(V(:))), A));
K(r,c)=K(r-1,c)
  댓글 수: 2
Isktaine
Isktaine 2012년 8월 8일
That's certainly more brief than my code, could you explain what V is?
Thomas
Thomas 2012년 8월 8일
@V creates an anonymous function. The cellfun documentation has more on it.. http://www.mathworks.com/help/techdoc/ref/cellfun.html

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

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by