Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
average of sections of an array

Subject: average of sections of an array

From: Daniel Robbins

Date: 20 Oct, 2012 10:14:08

Message: 1 of 3

Does anyone know how to calculate windows of means in a vector?

For example If I have a = [9 9 9 6 6 6 3 3 3] and I want to calculate the means in groups of three I would get b = [9 6 3]?

While for a short vector I could easily write something like:

b=[mean(a(1:3)) mean(a(4:6)) mean(a(7:9))]

The vectors I am working with are long so this becomes tedious, I am guessing there is a better way to script this?

Thanks in advance
Dan

Subject: average of sections of an array

From: Bruno Luong

Date: 20 Oct, 2012 10:41:08

Message: 2 of 3

"Daniel Robbins" <d.w.e.robbins@gmail.com> wrote in message <k5tthg$3nb$1@newscl01ah.mathworks.com>...
> Does anyone know how to calculate windows of means in a vector?
>
> For example If I have a = [9 9 9 6 6 6 3 3 3] and I want to calculate the means in groups of three I would get b = [9 6 3]?

a = [9 9 9 6 6 6 3 3 3]
b = mean(reshape(a,3,[]),1)

% Bruno

Subject: average of sections of an array

From: Daniel Robbins

Date: 20 Oct, 2012 11:08:08

Message: 3 of 3

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <k5tv44$8pf$1@newscl01ah.mathworks.com>...
> "Daniel Robbins" <d.w.e.robbins@gmail.com> wrote in message <k5tthg$3nb$1@newscl01ah.mathworks.com>...
> > Does anyone know how to calculate windows of means in a vector?
> >
> > For example If I have a = [9 9 9 6 6 6 3 3 3] and I want to calculate the means in groups of three I would get b = [9 6 3]?
>
> a = [9 9 9 6 6 6 3 3 3]
> b = mean(reshape(a,3,[]),1)
>
> % Bruno

Works perfectly, thanks Bruno.

Tags for this Thread

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.

Contact us