I have a time series, where there are some missing values. I've marked them as NaN. How would it be possible for me to interpolate them from the rest of the data. So my data to interpolate looks like that (just example numbers):
x=
0.482230405436799
0.0140930751890233
0.622880344434796
NaN
0.527433962776634
0.724991961357239
0.607415791144935
0.588366445795251
0.433434840033058
0.244172893507025
0.428960353778007
0.0101774555217771
0.608821449044360
0.957975188197358
NaN
0.0355905633801477
0.886235111325751
0.246941365057497
0.00891505625333700
0.814920271448039
0.140499436548767
0.879866440284003
0.0953767860905247
0.352560101248640
How to get a value for those NaN-s? I've tried Interp1 but i can't get it working properly.

1 commentaire

Jan
Jan le 3 Avr 2012
It is a good idea to post your trials with INTERP1, such that the problem can be fixed. This is usually easier that creating a solution from the scratch and you could learn, what went wrong.

Connectez-vous pour commenter.

 Réponse acceptée

Andrei Bobrov
Andrei Bobrov le 3 Avr 2012

5 votes

inn = ~isnan(x);
i1 = (1:numel(x)).';
pp = interp1(i1(inn),x(inn),'linear','pp')
out = fnval(pp,linspace(i1(1),i1(end),1000));
plot(i1,x,'ko',linspace(i1(1),i1(end),1000),out,'b-')
grid on

Plus de réponses (2)

Jan
Jan le 3 Avr 2012

14 votes

nanx = isnan(x);
t = 1:numel(x);
x(nanx) = interp1(t(~nanx), x(~nanx), t(nanx));
This does not catch NaNs at the margins, such that you have to care for them by your own, e.g. by repeating the first or last non-NaN value.

8 commentaires

Muhammad Usman Saleem
Muhammad Usman Saleem le 2 Fév 2016
@Jan Simon, Please tell me what interpolation technique your code is doing? like nearest, bilinear, cubic?
Also why its not interpolating bottom NaN values?
Jan
Jan le 2 Fév 2016
What are "bottom NaN values"? Why do you think, that they are not interpolated? The documentation of interp1 explains, that a linear interpolation is performed as default.
Stephen23
Stephen23 le 2 Fév 2016
See Muhammad Usman Saleem's Question, where he wanted to interpolate data (that includes NaN's).
Muhammad Usman Saleem
Muhammad Usman Saleem le 3 Fév 2016
Modifié(e) : Muhammad Usman Saleem le 3 Fév 2016
Thanks @Jan Simon.
Please go to my question where i have posted data set also with complete description of my question(as Stephen mentioned). Actually i am interpolating NaN values in text file these NaN values are only 3rd column of each text file. I search your way of interpolation in my code
% Read the file data:
S = dir('*Output_*.txt');
N = sort({S.name});
P = numel(N);
R = size(load(N{1},'-ascii'),1);
M = zeros(R,3,P);
for k = 1:P
M(:,:,k) = load(N{k},'-ascii');
end
tmp = 0==diff(M(:,1:2,:),1,3);
assert(all(tmp(:)),'First columns do not match')
% Rearrange:
[NA,NA,X] = unique(M(:,1,1));
[NA,NA,Y] = unique(M(:,2,1));
Z = reshape(M(:,3,:),max(X),max(Y),P);
% Detect -9999
Z(Z<-9998) = NaN;
nanx = isnan(Z);%logical vector for NaN
t = 1:numel(Z);%t run through length of z
Z(nanx) = interp1(t(~nanx), Z(~nanx), t(nanx));
after texting on one text file of my data, i have seen that all NaN values interpolated by this code but bottom NaN values are not interpolated? As you says, it will be linear interpolation by default then why when i replace this line with
Z(nanx) = interp1(t(~nanx), Z(~nanx), 'spline');
I am getting this error
??? In an assignment A(I) = B, the number of elements in B and
I must be the same.
Muhammad Usman Saleem
Muhammad Usman Saleem le 3 Fév 2016
Ok. I got my mistake and running finely this code. Only exporting of these interpolated columns to new text files remains.
Thanks Jan Simon
Royi Avital
Royi Avital le 20 Août 2017
Jan, how would you deal with 2D data (Still want to use MATLAB's interpolation functions and not FEX)?
Steven Lord
Steven Lord le 20 Août 2017
Check if the fillmissing function does what you want.
Matteo Soldini
Matteo Soldini le 13 Fév 2020
If I have 50 variables and I use Jan's code to interpolate each nan in each variable (variables have different ranges of values, for example var1 = [1,2], var2 = [300,400]), will it work or should I use a for loop?

Connectez-vous pour commenter.

John D'Errico
John D'Errico le 3 Avr 2012

9 votes

My inpaint_nans will fill them in with interpolated values, even if there are multiple consecutive nans, and it will extrapolate nicely at the ends. It works on 1-d problems.
x = inpaint_nans(x);

1 commentaire

Jan
Jan le 3 Avr 2012
+1. And it works on >1 D problems, where our direct INTERP1 approachs fail.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Interpolation of 2-D Selections in 3-D Grids dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by