Clear Filters
Clear Filters

Interpolating data from missing timesteps.

3 views (last 30 days)
I have 92 days of meteorological data with 4 samples per day, thus I should have 368 time steps. However, I currently only have 364 steps because one of the days in the middle of the sample is missing. The data is going to be used to condition another model so I need to have some kind of slightly reasonable data in the gap rather than NaN or 'O'.
The code that I'm trying to use is below, with the intention of creating a new array called 'PRECIP' extracting all of the data that I already have in 'PRECIP_CS' and inserting it into the correct place in the new array. The missing data should fit between step 156 and 157.
PRECIP = zeros(46,28,368);
for t=1:156
PRECIP(:,:,t) = PRECIP_CS(:,:,t);
end
for t=161:368
PRECIP(:,:,t) = PRECIP_CS(:,:,t-4);
end
t1=156;
t2=161;
for t=1:6;
for t3=t1:t2;
xt = t1:1:t2;
int1(:,:,t) = interp1(PRECIP_CS(:,:,t3),xt);
end
end
Currently the interpolant creates a new array that is 6x28x157 rather than 46x28x6 that I was hoping for. In addition the new array consists of NaN for the first 6 time steps and 0 for the rest. I know I must be doing something relatively simple wrong, I just can't work out what. Any assistance would be very welcome!

Accepted Answer

Andrew Newell
Andrew Newell on 23 Apr 2011
I'm not sure why you want an array that is 46x28x6. My interpretation of your question is that you have a 46x28 matrix of (spatial?) data for each time step, and you want to interpolate your 46x28x364 array to get a 46x28x368 array. You can do it without any loops as follows:
t0 = [1:156 161:368]; % your starting time steps
ti = 156:161; % the interpolated time steps
PRECIP_INT = permute(PRECIP_CS,[3 1 2]); % interp1 interpolates the first dimension of the array
PRECIP_INT = interp1(t0,PRECIP_INT,ti);
PRECIP_INT = permute(PRECIP_INT,[2 3 1]); % put the dimensions back the way they were.
EDIT: I modified the answer to give you just the 6 time steps.
  3 Comments
Andrew Newell
Andrew Newell on 23 Apr 2011
So why not just use ti=157:160?
Gordon Jones
Gordon Jones on 24 Apr 2011
Thanks, by using that method I've managed to get the data to work.

Sign in to comment.

More Answers (1)

Andrew Newell
Andrew Newell on 23 Apr 2011
This is Andrew's wife. I prefer to collapse the data gap, do the operation, then re-nanfill the data gap. (I do this when calculating an empirical orthogonal function using a gappy two dimensional data array). If you temporarily removed that one day of data, would your model work?

Products

Community Treasure Hunt

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

Start Hunting!