Interpolating within time series

1 vue (au cours des 30 derniers jours)
Luis Eduardo Cofré Lizama
Modifié(e) : Stephen23 le 20 Avr 2017
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

Réponse acceptée

Stephen23
Stephen23 le 20 Avr 2017
>> 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

Plus de réponses (1)

KSSV
KSSV le 20 Avr 2017
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 commentaire
Stephen23
Stephen23 le 20 Avr 2017
Modifié(e) : Stephen23 le 20 Avr 2017
See my answer for a simpler solution (just two simple lines of code).

Connectez-vous pour commenter.

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by