Calculate mean monthly from daily values
显示 更早的评论
I have a file that is 22281 rows x 5 col. This represents 60 years of daily values. I need to calculate mean monthly values for all years and extract, say, only May, to a new file. The resulting output will be 60 values for mean monthly data for May. This will be to compare col4 to col5 (below).
col1=year
col2=month
col3=day
col4=data1
col5=data2
Please help!
采纳的回答
更多回答(1 个)
Kostas
2012-1-26
maybe smt like this could work
suppose you have a matrix called data(22281,5) months are stored in second column
for i=1:12 %loop over months
idx=find(data(:,2)==i); % find all indeces for the specific month
meandata1=mean(idx,4);
meandata2=mean(idx,5);
meandata(i,:)=[i meandata1 meandata2]; %store in a new matrix the mean values per months
end
now you can just find and print in a file the values for the 5th month (May)
5 个评论
Brandi Newton
2012-11-25
There might be an error in this code? I'm not an expert, but i think in the third line you're asking the program to take the mean of the idx variable, in the 4th dimension: meandata1=mean(idx,4).
Instead you should ask the program to take the mean of the "data" variable, at the row indexes indicated by idx, with column specified, and in the 1st dimension (columns, 2nd dimension is rows).
So:
for i=1:1 %loop over months
idx=find(data(:,2)==i); % find all indeces for the specific month
meandata1=mean(data(idx,4),1); %CHANGES HERE, assuming data is in column 4
meandata(i,:)=[i meandata1]; %store in a new matrix the mean values per month
end
Does that make sense? It seems to be working for me now.
Thanks!
Brandi Newton
2012-11-25
oh hang on, the code i wrote averages all days in all januaries. I need it for each year...
per isakson
2012-11-25
Faisal Amri
2017-11-23
I Tried to modify, this what I got and it works for me though I still working to make it better. Hope this helps
k=1
%Calculating Monthly Average
for j=1992:2012 %Years Looping
for i=1:12 %Months Looping
idx=find((Data(:,1)==i) & (Data(:,3)==j)); %Find all indeces for the specific month
meandata1=mean(Data(idx,4),1);
Monthly_Ave(k,:)=[i,j,meandata1;]; %Store in a new matrix the mean values per month
k=k+1;
end
end
frankovaT
2018-9-27
Unfortunately this is not working either
类别
在 帮助中心 和 File Exchange 中查找有关 MATLAB 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!