How to interpolate and leave NaNs if long gaps?
이전 댓글 표시
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
채택된 답변
추가 답변 (1개)
Dondon Gallentes
2023년 4월 3일
0 개 추천
use fillmissing() function and specify the maximum gap with 'MaxGap'
카테고리
도움말 센터 및 File Exchange에서 Interpolation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!