replacing NaN with it previous element

1 view (last 30 days)
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

Accepted Answer

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

More Answers (2)

Isktaine
Isktaine on 8 Aug 2012
Edited: Isktaine on 8 Aug 2012
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 Comments
John Petersen
John Petersen on 8 Aug 2012
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 on 8 Aug 2012
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! :)

Sign in to comment.


Thomas
Thomas on 8 Aug 2012
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 Comments
Isktaine
Isktaine on 8 Aug 2012
That's certainly more brief than my code, could you explain what V is?
Thomas
Thomas on 8 Aug 2012
@V creates an anonymous function. The cellfun documentation has more on it.. http://www.mathworks.com/help/techdoc/ref/cellfun.html

Sign in to comment.

Categories

Find more on Multidimensional Arrays in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!