Interpolating within time series

6 次查看(过去 30 天)
I need to replace NaNs in a long time series that has consecutive (varying lengths) and non-consecutive NaNs. I tried the solution in this link https://au.mathworks.com/matlabcentral/answers/160575-replacing-nan-values-with-average-values-of-the-nearest-numbers but it doesn't seem to work for all my NaNs (not familiar with the bsxfun @(x,y)). Thanks for your help,
1438.60000000000
1446.53000000000
NaN
1426.85000000000
1414.34000000000
1419.98000000000
1418.46000000000
1420.29000000000
1426.09000000000
1422.12000000000
NaN
NaN
1464.23000000000
1459.20000000000
1463.32000000000
1474.46000000000
1460.27000000000
1441.50000000000
1442.72000000000
1446.23000000000
1443.48000000000
1444.55000000000
1443.79000000000
1444.55000000000
NaN
NaN
NaN
1484.07000000000
1482.54000000000

采纳的回答

Stephen23
Stephen23 2017-4-20
>> idx = ~isnan(A);
>> interp1(find(idx),A(idx),(1:numel(A))')
ans =
1438.6
1446.5
1436.7
1426.8
1414.3
1420
1418.5
1420.3
1426.1
1422.1
1436.2
1450.2
1464.2
1459.2
1463.3
1474.5
1460.3
1441.5
1442.7
1446.2
1443.5
1444.6
1443.8
1444.6
1454.4
1464.3
1474.2
1484.1
1482.5

更多回答(1 个)

KSSV
KSSV 2017-4-20
A = [1438.60000000000
1446.53000000000
NaN
1426.85000000000
1414.34000000000
1419.98000000000
1418.46000000000
1420.29000000000
1426.09000000000
1422.12000000000
NaN
NaN
1464.23000000000
1459.20000000000
1463.32000000000
1474.46000000000
1460.27000000000
1441.50000000000
1442.72000000000
1446.23000000000
1443.48000000000
1444.55000000000
1443.79000000000
1444.55000000000
NaN
NaN
NaN
1484.07000000000
1482.54000000000] ;
% positions
idx = 1:length(A) ;
% get NaN positions
idx_nan = find(isnan(A)) ;
% non nan's positions
idx_nonnan = find(~isnan(A)) ;
% do interpolation
A_nonnan = A(idx_nonnan) ;
A_nan = interp1(idx_nonnan,A_nonnan,idx_nan) ;
%%replace NaNs
iwant = A ;
iwant(idx_nan) = A_nan ;
% plot
plot(idx,A,'r')
hold on
plot(idx,iwant,'b') ;
legend('with Nans', 'NaNs filled')
  1 个评论
Stephen23
Stephen23 2017-4-20
编辑:Stephen23 2017-4-20
See my answer for a simpler solution (just two simple lines of code).

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Multirate Signal Processing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by