Calculate mean from daily to monthly values

조회 수: 5 (최근 30일)
minh lan
minh lan 2019년 5월 16일
답변: ABHILASH SINGH 2020년 3월 13일
I have I have a data containing daily pricipitation data from 2001 to 2010, each year I have 1 file
lon=ncread(ncfile,'lon');
lat=ncread(ncfile,'lat');
precip=ncread(ncfile,'precip');
precip 360 x 280 x 365
lat 280x1
lon 360x1
I want to calculate the monthly mean for each year and then calculate average for 10 years.
the slow way I can do like that:
JAN{K}=nansum(precip{K}(:,:,1:31),3); %K is number of year
FEB{K}=nansum(precip{K}(:,:,32:59),3);
JAN=cat(3,JAN{:});
JAN=mean(JAN,3);
but for 2004, 2008 we have 366 day and this way is not very good to calculate
How can I do that with loop?
Thanks

채택된 답변

convert_to_metric
convert_to_metric 2019년 5월 16일
Hi minh lan,
You can consider making use of the month funciton. Take a look at the following code, perhaps it will lead you to a solution.
data=[1:365]*10; % just some example data
length_of_precip_data=length(data); % you might need to use the size function in place of length depending on how your data is organized
year=2010; % for example
start=datetime(year,1,1);
m=month(start+caldays(1:length_of_precip_data)-1); % now you have an array the same size as your data that indicates which month an element is in
% so if you want to find just values from february:
data(m==2) % february is the second month, ie m==2

추가 답변 (2개)

Steven Lord
Steven Lord 2019년 5월 16일
Use the groupsummary function or store your data in a timetable and use retime on that timetable.

ABHILASH SINGH
ABHILASH SINGH 2020년 3월 13일

카테고리

Help CenterFile Exchange에서 Data Preprocessing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by