Creating a monthly average from a matrix
2 次查看(过去 30 天)
显示 更早的评论
My data, which has 30 years of monthly data, over a 2 degree grid of the globe:
>> whos KoppenA_prate
Name Size Bytes Class Attributes
KoppenA_prate 90x180x360 23328000 single
Not every pixel has data, just the pixels that fit into Koppen classification A
>> sum(A(:))
ans =
559
My question: Any ideas on how I could write a loop to create an average monthly value over the 30 years?
Thanks a bunch!
采纳的回答
Cedric
2013-8-21
编辑:Cedric
2013-8-21
Just
avgAllYears = mean(KoppenA_prate, 3) ;
where 3 is the dimension along which the mean must be computed. With that you get a 90x180 array of means over all times.
EDIT:
siz = size(KoppenA_prate) ;
avg = zeros(siz(1), siz(2), 12) ;
for mId = 1 : 12
avg(:,:,mId) = mean(KoppenA_prate(:,:,mId:12:end), 3) ;
end
EDIT2: or, more efficient but more complicated
siz = size(KoppenA_prate) ;
buf = mean(reshape(KoppenA_prate, siz(1), 12*siz(2), []), 3) ;
avg = reshape(buf, siz(1), siz(2), []) ;
Note: both produce a 90x180x12 array of monthly averages, were e.g. avg(:,:,5) is the average of 5th months over all years.
4 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Time Series Events 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!