How to interpolate under specific condition?

5 ビュー (過去 30 日間)
Daphne PARLIARI
Daphne PARLIARI 2021 年 1 月 13 日
コメント済み: Daphne PARLIARI 2021 年 1 月 14 日
Hi everyone. I would appreciate your help on this.
I have a daily timeseries of temperature observations for 2006-2016.
I want to reconstruct missing values (see attached) with an interpolation method only if the missing values cover less that 3 days. If the gap is larger (>4days), I want NaN values to be preserved. Column A contains the dates I have temperature observations (NaT indicates temperature is NaN) and Column C is the full time sequence 2006-2016.
How can I do this? I am totally confused... The interpolation method could be anything, I have not decided on this yet.
Thank you in advance!
PS. I am on R2019a.

採用された回答

Stephan Ciobanu
Stephan Ciobanu 2021 年 1 月 13 日
編集済み: Stephan Ciobanu 2021 年 1 月 13 日
Hi, this code should work:
The file Daily data.xlsx must be in the same directory as your script!
DATA = importdata('Daily data.xlsx'); % importing all data
MeanTemp = DATA.data.Sheet1; % creating an array containg the mean Temp measured
pos = find(isnan(MeanTemp)); % find NaN elements
x_val = (1:length(MeanTemp))'; % creatin a 'pseudo-timeline' vector
for i = 4:length(pos) % finding the index of 4 or more consecutive NaN
if (pos(i)==pos(i-2)+2 && pos(i)==pos(i-1)+1 && pos(i)==pos(i-3)+3)
consec_days(i)=true;
end
end
consec_nan = find(consec_days==true); % index of pos where we want to keep NaN
y = pos;
y(consec_nan-3) = 0;
y(consec_nan-2) = 0;
y(consec_nan-1) = 0;
y(consec_nan) = 0;
k = find(y==0); % excluding 4 or more consecutive days in interpolation
y(k)=[]; % NaN to be interpolated
%--------INTERPOLATION-----------------%
MeanTemp(pos)=[]; % values to be interpolated
x_val(pos)=[];
x_unknown_temp = [x_val;y];
x_unknown_temp = sort(x_unknown_temp);
MeanTemp_interpolated = spline(x_val,MeanTemp,x_unknown_temp);
MeanTemp_interpolated1 = interp1(x_val,MeanTemp,x_unknown_temp);
%---------INTERPOLATION-END-------------%
% Creating a matrix with 2 rows
% containg the values of NaN interpolated
% in both cases
% (spline first column, interp1 second column)
Values_interpolated(:,1) = MeanTemp_interpolated(y);
Values_interpolated(:,2) = MeanTemp_interpolated1(y);
% Date-time line below:
datetime1 = datetime(2006,01,01):caldays(1):datetime(2016,12,31);
% String_Values_interpolated contains Values_interpolated
% and the relative day
String_Values_interpolated = num2cell(Values_interpolated(1:end,1:2));
String_Values_interpolated(:,3) = cellstr(datetime1(y))';
  5 件のコメント
Stephan Ciobanu
Stephan Ciobanu 2021 年 1 月 14 日
編集済み: Stephan Ciobanu 2021 年 1 月 14 日
It might be, I'm using R2020b and it works.
Using the script above you can add those lines at the end:
Temp=DATA.data.Sheet1;
Temp(y)=MeanTemp_interpolated(y)
% or
% Temp(y)=MeanTemp_interpolated1(y)
Daphne PARLIARI
Daphne PARLIARI 2021 年 1 月 14 日
I am sorry, where should I add the script above?

サインインしてコメントする。

その他の回答 (1 件)

Steven Lord
Steven Lord 2021 年 1 月 14 日
If you were using release R2020b or later, I would recommend calling the fillmissing function with the 'MaxGap' option. I'm leaving this as an answer even though you're using release R2019a for future reference by other users or in case you can and are willing to upgrade.
  1 件のコメント
Daphne PARLIARI
Daphne PARLIARI 2021 年 1 月 14 日
Yes you are absolutely right. MaxGap doesn't work for me but it is exactly what I need.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeData Preprocessing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by