Suppose you have a dataset with precipitation data in a vector precipitation
, and a corresponding vector months
indicating the month of each data point. You can compute the average precipitation for each month using the accumarray
function. Here's how you can do it:
main.m580 chars11 lines
In this code, accumarray
takes three inputs:
months'
(note the transpose) is the vector of subscripts that accumarray
uses to group the precipitation
data by month.precipitation'
(note the transpose again) is the vector of values to be averaged.[]
tells accumarray
to create a new output array, one element for each month.@mean
is a function handle that tells accumarray
to compute the mean of each group.The resulting monthly_averages
variable should be a row vector with 12 elements, one for each month. The first element will be the average precipitation for January, the second element will be the average precipitation for February, and so on.
gistlibby LogSnag