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

missing values

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

Best Answer

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!