Creating a monthly average from a matrix

조회 수: 2 (최근 30일)
Kate
Kate 2013년 8월 20일
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!
  댓글 수: 5
Kate
Kate 2013년 8월 21일
I would like to create a vector of precip data, for each month averaged over 30 years.
Kate
Kate 2013년 8월 21일
This is as far as I've gotten, but my for loop isn't working:
%Create monthly averages over 30 years
Jan=[1:12:349];
Feb=[2:12:350];
Mar=[3:12:351];
Apr=[4:12:352];
May=[5:12:353];
Jun=[6:12:354];
Jul=[7:12:355];
Aug=[8:12:356];
Sep=[9:12:357];
Oct=[10:12:358];
Nov=[11:12:359];
Dec=[12:12:360];
for ii=length(num_mons)
if num_mons==Jan;
Jan_dat=[Jan,KoppenA_prate(3)];
elseif num_mons==Feb;
Feb_dat=[Feb,KoppenA_prate(3)];
end
end

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

채택된 답변

Cedric
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
Kate
Kate 2013년 8월 21일
Thanks Cedric, that's great. How could I get a single, say Jan, value over the whole grid? I need to plot average yearly precip.
Thanks for all of your help!
Cedric
Cedric 2013년 8월 21일
You're welcome; see my last note.

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

추가 답변 (0개)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by