MATLAB: Calculate monthly averages of time series

mean

I have a dataset which contains measurements taken at discrete intervals for the past ~ 40 years. I would like to calculate the monthly mean values from the measurements. Consider the following example:
DateTime=datestr(datenum('1970-01-01','yyyy-mm-dd'):1:...
datenum('2011-12-31','yyyy-mm-dd'),...
'yyyy-mm-dd');
DateTime=cellstr(DateTime);
DateVec = datevec(DateTime);
dt = [1,4,32,232,101,44,23,1,87,843,456];
DateVec(dt,:) = [];
Data = rand(1,length(DateVec(:,1)));
Here, I have excluded some of the Dates in order to specify that the intervals of measurements were not continuous, obviously the measurements are far more spread out than shown above. So, what I would like to do is to use the date vector to specify each year and then calculate the mean of each month for a given year so that at the end I would have 12 measurements for each year.

Best Answer

[a,b,b] = unique(DateVec(:,1:2),'rows');
out = [a,accumarray(b,Data,[],@mean)]; %EDIT row
or
[a1,b1,b1] = unique(DateVec(:,1));
[a2,b2,b2] = unique(DateVec(:,2));
out = [nan,a2';a1,accumarray([b1,b2],Data,[],@mean)]; % EDIT row