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.

 채택된 답변

Andrei Bobrov
Andrei Bobrov 2012년 7월 5일
편집: Andrei Bobrov 2012년 7월 5일

1 개 추천

[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

댓글 수: 3

Richard
Richard 2012년 7월 5일
편집: Richard 2012년 7월 5일
many thanks. From this, to calculate the average from each month, not the accumulation I could write:
NewVal = cell2mat(arrayfun(@(x)length(find(b == x)),unique(b),'un',0));
out(:,3) = out(:,3)./NewVal;
Is this correct?
Andrei Bobrov
Andrei Bobrov 2012년 7월 5일
Sorry! I edited my answer. Please read about MATLAB's function accumarray.
Benju Baniya
Benju Baniya 2022년 7월 12일
Hi, i am new to matlab and trying to work with timeseries data. What does a, b, b mean here? My data looks like as shown below. How do I calculate GPP based on month so that I get 12 values for each year? Thanks in advance.
YEAR TIMESTAMP DoY NEE GPP
2013 201301010030 1 -0.0064 0.0025
2013 201301010100 2 - 0.0076 0.0026
2014 201401010030 1 0.0983 -0.0076
2014 201301010100 2 0.0075 - 0.0089

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터 및 File Exchange에서 Dates and Time에 대해 자세히 알아보기

태그

질문:

2012년 7월 5일

댓글:

2022년 7월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by