Missing value: Adding new row of NaN in a time series. Need some twist in code

Dear all,
I want to create some new rows of NaN (lets say NaN(1,:,:)) in gridded time series (e.g., A_matrix (200,40,50)) to fill up some missing months in between. I have identified the missing months using:
[missingvalues,id] = setdiff(actual_period,data_dates);
where id is the index of missing month OR
[locCol, loc] = ismember(actual_period,data_dates);
where loc is an index of logical values for 0=missing month.
Based on the index values I need to add extra rows (NaNs) to A_matrix so that I can interpolate them.
Kindly help me... Khandu

2 件のコメント

Simon
Simon 2013 年 10 月 15 日
Hi!
Do you have some kind of example code to show us?
khandu
khandu 2013 年 10 月 15 日
HI Simon, Thanks for the response. I have the following code as an example:
%%A_matrix contains the real observation at time intervals defined by data_period
% it contains missing data at various intervals
% nmonths refers the months and extra raw needs to be added to match with
% actual_period
A_matrix = rand(122,20,30);
[nmonths, nlat, nlon] = size(A_matrix);
% data period
% convert to decimal year using year and month functions from fnancial
% toolbox
data_period = datenum(2003,[1:7, 10:35, 38:126],1); % containing missing values at various intervals
data_dates = year(data_period)+month(data_period)/12;
% study period
actual_period = datenum(2003,1:126,1); %%period defining the study length
actual_dates = year(actual_period)+ month(actual_period)/12;
[locCol, loc] = ismember(actual_dates,data_dates);
% missing time series shall be replace by NaN
missing_dat = NaN(1,nlat, nlon);
% index mising data
[missingvalues,id] = setdiff(actual_dates,data_dates);
% using ismember
From here I m trying to replace the missing timeseries with missing_dat using the index but stuck for sometime.
I hope you can help it.I hope you have the year month function.
Thanks Khandu

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

 採用された回答

sixwwwwww
sixwwwwww 2013 年 10 月 15 日
Dear Khandu, you can insert NaN at missing location as follows:
A_matrix = rand(122,20,30);
[nmonths, nlat, nlon] = size(A_matrix);
data_period = datenum(2003,[1:7, 10:35, 38:126],1);
data_dates = year(data_period)+month(data_period)/12;
actual_period = datenum(2003,1:126,1);
actual_dates = year(actual_period)+ month(actual_period)/12;
missing_dat = NaN(1,nlat, nlon);
[missingvalues,id] = setdiff(actual_dates,data_dates);
ExtraMonths = length(id);
B = zeros(nmonths+ExtraMonths, nlat, nlon);
A_index = 1;
for i = 1:nmonths+ExtraMonths
if sum(ismember(id, i)) == 1
B(i, :, :) = missing_dat;
else
B(i, :, :) = A_matrix(A_index, :, :);
A_index = A_index + 1;
end
end
I hope it helps. Good luck!

2 件のコメント

khandu
khandu 2013 年 10 月 15 日
Hi there,
Great one. Thanks.
Khans
sixwwwwww
sixwwwwww 2013 年 10 月 15 日
You are welcome

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

その他の回答 (0 件)

カテゴリ

質問済み:

2013 年 10 月 15 日

コメント済み:

2013 年 10 月 15 日

Community Treasure Hunt

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

Start Hunting!

Translated by