# How to interpolate and leave NaNs if long gaps?

20 ビュー (過去 30 日間)
K E 2015 年 8 月 5 日

I have a temperature measurement (x) which is sampled nearly-regularly in time (t), except for data dropouts (missing t,x values). I want to make an interpolated xi which is regular in time, but indicate data dropouts with with NaNs rather than interpolating across the gaps. How do I do this? I would like to avoid looping through each interpolated value and replacing it with a NaN if it is far from any measurement (real data vector is long).
x=rand(1,60); % Fake temperature measurement
t=[1:10 21:30 41:50 61:70 81:90 101:110]; % Time samples with dropouts, e.g. t=11,12
tNoise = t + rand(1,length(t))/100; % Time step varies slightly between measurements so add a little noise
timeStep=median(diff(t)); % Time step if there were no dropouts
ti=min(tNoise):timeStep:max(tNoise); % Time vector without dropouts
xi=interp1(tNoise,x,ti); % No NaNs, so dropouts are filled with 'fake' data
% How to replace fake data with NaNs if xi value is more than timeStep away from any value in t?
plot(ti,xi, 'b.', tNoise,x, 'co'); % Show fake data between actual sample time

サインインしてコメントする。

### 採用された回答

David Young 2015 年 8 月 5 日
I'd adopt a slightly different approach. I'd quantize the times first to get an array of just the times you want, then interpolate into those times only. The quantization gives you also the indices of the times in an array of evenly-spaced times. Then you can copy the interpolated data into just those bits of the array. The approach assumes that the noise in the times is small compared to the time difference.
My version looks like this.
% Data (with t starting at -5 to demonstrate generality)
x=rand(1,60); % Fake temperature measurement
t=[-5:4 21:30 41:50 61:70 81:90 101:110]; % Time samples with dropouts, e.g. t=11,12
tNoise = t + rand(1,length(t))/100; % Time step varies slightly between measurements so add a little noise
timeStep=median(diff(t)); % Time step if there were no dropouts
% quantise tNoise
t1 = tNoise(1); % starting time
tCount = round((tNoise-t1)/timeStep); % time in units of timeStep
tIndex = tCount + 1; % index of these times in array of regular times
tQuant = t1 + timeStep * tCount; % time to nearest timeStep
% interpolate for these points only (need extrapolation for first or last)
xiAtTquant = interp1(tNoise, x, tQuant, 'linear', 'extrap');
% evenly spaced times for whole sequence
ti = linspace(tQuant(1), tQuant(end), tIndex(end));
% output array of x values
xi = NaN(1, tIndex(end));
xi(tIndex) = xiAtTquant;
% plot
plot(ti,xi, 'bx', tNoise,x, 'co'); % Show fake data between actual sample time
##### 2 件のコメントなしを表示なしを非表示
K E 2015 年 8 月 5 日

This works and is much faster than looping through each value. I will use this all the time.
Andrew Pullin 2017 年 2 月 7 日
This was an immensely helpful answer. I am surprised that a function like this does not already exist in Matlab, so that datasets can be treated blindly as large blocks. In Mathematica, you can plot with "exclusions", which will do the gap skipping.
You should generalize this function into a "gapify" function and put it on the File Exchange. Or I should do that ...

サインインしてコメントする。

### その他の回答 (1 件)

Dondon Gallentes 2023 年 4 月 3 日
use fillmissing() function and specify the maximum gap with 'MaxGap'

サインインしてコメントする。

### カテゴリ

Help Center および File ExchangeInterpolation についてさらに検索

### Community Treasure Hunt

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

Start Hunting!

Translated by