Asked by Kate
on 20 Aug 2013

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!

*No products are associated with this question.*

Answer by Cedric Wannaz
on 21 Aug 2013

Edited by Cedric Wannaz
on 21 Aug 2013

Accepted answer

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.

Show 1 older comment

Cedric Wannaz
on 21 Aug 2013

Ok, I misunderstood "monthly average" for "average over monthly values". See my edits.

Opportunities for recent engineering grads.

## 5 Comments

## Azzi Abdelmalek (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/85209#comment_165192

This is not clear for me. What are your data? and what do you want?

## Image Analyst (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/85209#comment_165207

What does each of the 3 dimensions represent? Is any one of the dimensions the time point? If so, are the time points all one month apart? Or, are they days of the year, so that one month might have 30 data points sometimes, and 31 points at other times?

## Kate (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/85209#comment_165333

90x180 is a 2 degree grid of the globe, 360 is 12 months x 30 years

## Kate (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/85209#comment_165336

I would like to create a vector of precip data, for each month averaged over 30 years.

## Kate (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/85209#comment_165350

This is as far as I've gotten, but my for loop isn't working: