Interpolating within time series
1 次查看(过去 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
0 个评论
采纳的回答
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
0 个评论
更多回答(1 个)
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!