Thread Subject: average every 12th row of matrix

Subject: average every 12th row of matrix

From: Nix Nicholson

Date: 5 Nov, 2009 17:19:03

Message: 1 of 6

Hi: Beginner question - I have 7 years of some monthly data fields and I want to compute the average for each month over the 7 year period. I can do it by specifying row numbers but how could I do this so that come next year, when i have 8 years data I can just change a scaler value for year number and it will recompute a new average?
basically how do i compute a mean of every nth row, in my case every 12th.

Many thanks

Subject: average every 12th row of matrix

From: the cyclist

Date: 5 Nov, 2009 17:30:35

Message: 2 of 6

"Nix Nicholson" <linznix@gmail.com> wrote in message <hcv1e7$3ua$1@fred.mathworks.com>...
> Hi: Beginner question - I have 7 years of some monthly data fields and I want to compute the average for each month over the 7 year period. I can do it by specifying row numbers but how could I do this so that come next year, when i have 8 years data I can just change a scaler value for year number and it will recompute a new average?
> basically how do i compute a mean of every nth row, in my case every 12th.
>
> Many thanks

You should be able to user the FILTER command to do this.

Subject: average every 12th row of matrix

From: Matt

Date: 5 Nov, 2009 17:30:36

Message: 3 of 6

"Nix Nicholson" <linznix@gmail.com> wrote in message <hcv1e7$3ua$1@fred.mathworks.com>...
> Hi: Beginner question - I have 7 years of some monthly data fields and I want to compute the average for each month over the 7 year period. I can do it by specifying row numbers but how could I do this so that come next year, when i have 8 years data I can just change a scaler value for year number and it will recompute a new average?
> basically how do i compute a mean of every nth row, in my case every 12th.

Instead of organizing your data as a vector, you can organize it as an Nx12 matrix A where N is the number of years. Then do

MonthlyMeans=mean(A,1);

When you have N=8, just add a row and do the same thing.

Subject: average every 12th row of matrix

From: dpb

Date: 5 Nov, 2009 17:26:55

Message: 4 of 6

Nix Nicholson wrote:
> Hi: Beginner question - I have 7 years of some monthly data fields
> and I want to compute the average for each month over the 7 year
> period. I can do it by specifying row numbers but how could I do this
> so that come next year, when i have 8 years data I can just change a
> scaler value for year number and it will recompute a new average?
> basically how do i compute a mean of every nth row, in my case every
> 12th.
...

idx=1:12:length(x);
avg = mean(idx,:);

Salt to suit on selecting starting month, etc., ...

--

Subject: average every 12th row of matrix

From: dpb

Date: 5 Nov, 2009 18:32:02

Message: 5 of 6

dpb wrote:
> Nix Nicholson wrote:
>> Hi: Beginner question - I have 7 years of some monthly data fields
>> and I want to compute the average for each month over the 7 year
>> period. I can do it by specifying row numbers but how could I do this
>> so that come next year, when i have 8 years data I can just change a
>> scaler value for year number and it will recompute a new average?
>> basically how do i compute a mean of every nth row, in my case every
>> 12th.
> ...
>
> idx=1:12:length(x);
> avg = mean(idx,:);
>
> Salt to suit on selecting starting month, etc., ...

Should have been

avg = mean(x(idx,:));

of course...

--

Subject: average every 12th row of matrix

From: Lindsey

Date: 1 Sep, 2011 10:29:13

Message: 6 of 6

Hello, thanks to you all.

I used dpb's solution written out as:

for i=1:12; monthmean(i,:)=mean(x([i:12:end],:)); end

where x is my original dataset with each row representing a month value and each column representing a variable.

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

Contact us at files@mathworks.com