replacing NaN with it previous element

2 visualizaciones (últimos 30 días)
antonet
antonet el 8 de Ag. de 2012
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

Respuesta aceptada

Azzi Abdelmalek
Azzi Abdelmalek el 8 de Ag. de 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

Más respuestas (2)

Isktaine
Isktaine el 8 de Ag. de 2012
Editada: Isktaine el 8 de Ag. de 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 comentarios
John Petersen
John Petersen el 8 de Ag. de 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 el 8 de Ag. de 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! :)

Iniciar sesión para comentar.


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

Iniciar sesión para comentar.

Categorías

Más información sobre Dates and Time en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by