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 Kommentare

Hi!
Do you have some kind of example code to show us?
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

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

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!

Weitere Antworten (0)

Kategorien

Gefragt:

am 15 Okt. 2013

Kommentiert:

am 15 Okt. 2013

Community Treasure Hunt

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

Start Hunting!

Translated by