# Q：用matlab每日平均数据（栅格）每月平均值

I have 2192 rasters representing daily timestep climate data (1 jan 2008-31 mar 2013), which I would like to aggregate into monthly means (i.e average all the data within a given month&year).

I can load the data into Matlab fine to create a 3D matrix, of size 110x57x2192. The 2192 represents time dimension from jan 2008-31 to 31 mar 2013.

I have tried this using numbering in excel, but I am not sure how to set the syntax right. The monthly means should idealy replace the imported daily data in the memory. My numbering in Excel has four columns: date,day, month and year. Any NA pixel is ignored and the average for the pixel based on non NA pixels for a given month.

``````%create monthly averages from daily data
month=numbering(:,2);
years=unique(year);
``````

Here is the first three and last three row of my excel file for numbering

1/01/2008 1 1 2008
2/01/2008 2 1 2008
3/01/2008 3 1 2008
29/12/2013 29 12 2013
30/12/2013 30 12 2013
31/12/2013 31 12 2013
31/12/2013 31 12 2013

``````%create monthly averages from daily data
month=numbering(:,2);
years=unique(year);
``````

1/01/2008 1 1 2008
2/01/2008 2 1 2008
3/01/2008 3 1 2008
29/12/2013 29 12 2013
30/12/2013 30 12 2013
31/12/2013 31 12 2013
31/12/2013 31 12 2013

Here's how I would tackle the problem:

``````% Getting month breaks by detecting changes in days
day=numbering(:,2); % Day column from Excel
final_idx = find(diff(day)~=1);
initial_idx = [1 ; final_idx(1:end-1)-1];

%Number of months
nom = length(final_idx);

% Pre-allocating space for monthly mean
monthlymeans = nan(110,57,nom);

% Calculating means
for ii=1:nom
monthlymeans(:,:,ii) = nanmean(3dmatrix(:,:,initial_idx(ii):final_idx(ii)),3);
end
``````

Note that I assumed the data is contained in a variable called 3dmatrix and the NA pixel values are actually NaN's, since I do not have access to your original data.

``````% Getting month breaks by detecting changes in days
day=numbering(:,2); % Day column from Excel
final_idx = find(diff(day)~=1);
initial_idx = [1 ; final_idx(1:end-1)-1];

%Number of months
nom = length(final_idx);

% Pre-allocating space for monthly mean
monthlymeans = nan(110,57,nom);

% Calculating means
for ii=1:nom
monthlymeans(:,:,ii) = nanmean(3dmatrix(:,:,initial_idx(ii):final_idx(ii)),3);
end
``````

matlab