Interpolating within time series

조회 수: 3 (최근 30일)
Luis Eduardo Cofré Lizama
Luis Eduardo Cofré Lizama 2017년 4월 20일
편집: Stephen23 2017년 4월 20일
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에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by