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 problem

Subject: average problem

From: Adam Bright

Date: 26 Mar, 2008 18:26:02

Message: 1 of 10

Hi there, this may seem really trivial but i seem to be
stuck to perform the follwing command for my array.

how do i say to provide an average value between every 7 values?

e.g in excel I have this:
=AVERAGE(I1:I8)
=AVERAGE(I2:I9)
=AVERAGE(I3:I10)
...

Thanks for your help!

Subject: average problem

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 26 Mar, 2008 18:39:58

Message: 2 of 10

In article <fse4fq$gsq$1@fred.mathworks.com>,
Adam Bright <abc_services_2000@yahoo.co.uk> wrote:
>Hi there, this may seem really trivial but i seem to be
>stuck to perform the follwing command for my array.

>how do i say to provide an average value between every 7 values?

Is the vector an exact multiple of 7 long? If so then reshape
it to be 7 x whatever, and take the mean() along the first dimension.

If it is not an exact multiple of 7 long, then you will have to
decide how you want to handle the last incomplete group.
--
  "Pray do not take the pains / To set me right. /
   In vain my faults ye quote; / I wrote as others wrote /
   On Sunium's hight." -- Walter Savage Landor

Subject: average problem

From: Roger Stafford

Date: 26 Mar, 2008 18:44:03

Message: 3 of 10

"Adam Bright" <abc_services_2000@yahoo.co.uk> wrote in message <fse4fq
$gsq$1@fred.mathworks.com>...
> Hi there, this may seem really trivial but i seem to be
> stuck to perform the follwing command for my array.
>
> how do i say to provide an average value between every 7 values?
>
> e.g in excel I have this:
> =AVERAGE(I1:I8)
> =AVERAGE(I2:I9)
> =AVERAGE(I3:I10)
> ...
>
> Thanks for your help!
-------
  Do you want averages over 7 or over 8 elements? This is for 8:

 av = mean(hankel(I(1:8),I(8:end)));

Roger Stafford

Subject: average problem

From: Adam Bright

Date: 26 Mar, 2008 18:49:01

Message: 4 of 10

Hi thanks for your quick response, the answer is no the
array will not always be an exact multiple of 7, and ideally
for the remaining values they can just be averaged amongest
themselves, i.e if theres 3 remaing then an average of
that,but i am open to ideas.


roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in
message <fse59u$n1r$1@canopus.cc.umanitoba.ca>...
> In article <fse4fq$gsq$1@fred.mathworks.com>,
> Adam Bright <abc_services_2000@yahoo.co.uk> wrote:
> >Hi there, this may seem really trivial but i seem to be
> >stuck to perform the follwing command for my array.
>
> >how do i say to provide an average value between every 7
values?
>
> Is the vector an exact multiple of 7 long? If so then reshape
> it to be 7 x whatever, and take the mean() along the first
dimension.
>
> If it is not an exact multiple of 7 long, then you will
have to
> decide how you want to handle the last incomplete group.
> --
> "Pray do not take the pains / To set me right. /
> In vain my faults ye quote; / I wrote as others wrote /
> On Sunium's hight." -- Walter
Savage Landor

Subject: average problem

From: Bjoern Eskofier

Date: 26 Mar, 2008 18:50:18

Message: 5 of 10

"Adam Bright" <abc_services_2000@yahoo.co.uk> wrote in
message <fse4fq$gsq$1@fred.mathworks.com>...
> Hi there, this may seem really trivial but i seem to be
> stuck to perform the follwing command for my array.
>
> how do i say to provide an average value between every 7
values?
>
> e.g in excel I have this:
> =AVERAGE(I1:I8)
> =AVERAGE(I2:I9)
> =AVERAGE(I3:I10)
> ...
>
> Thanks for your help!


Y = filter(1/7*[1 1 1 1 1 1 1],1,X) where X is your input
array does the trick...
Be careful at the edges of the array though.

>> help filter

will tell you more!


Cheers,
Bjoern

Subject: average problem

From: Adam Bright

Date: 26 Mar, 2008 18:50:18

Message: 6 of 10

Thanks for that

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid>
wrote in message <fse5hj$2qh$1@fred.mathworks.com>...
> "Adam Bright" <abc_services_2000@yahoo.co.uk> wrote in
message <fse4fq
> $gsq$1@fred.mathworks.com>...
> > Hi there, this may seem really trivial but i seem to be
> > stuck to perform the follwing command for my array.
> >
> > how do i say to provide an average value between every 7
values?
> >
> > e.g in excel I have this:
> > =AVERAGE(I1:I8)
> > =AVERAGE(I2:I9)
> > =AVERAGE(I3:I10)
> > ...
> >
> > Thanks for your help!
> -------
> Do you want averages over 7 or over 8 elements? This is
for 8:
>
> av = mean(hankel(I(1:8),I(8:end)));
>
> Roger Stafford
>

Subject: average problem

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 26 Mar, 2008 18:55:24

Message: 7 of 10

In article <fse59u$n1r$1@canopus.cc.umanitoba.ca>,
Walter Roberson <roberson@ibd.nrc-cnrc.gc.ca> wrote:
>In article <fse4fq$gsq$1@fred.mathworks.com>,
>Adam Bright <abc_services_2000@yahoo.co.uk> wrote:

>>how do i say to provide an average value between every 7 values?

>Is the vector an exact multiple of 7 long? If so then reshape
>it to be 7 x whatever, and take the mean() along the first dimension.

Ah, that answer was for each -group- of 7 values. What you want
is more commonly called a "moving average".

W = 7;
cs = [0 cumsum(A)];
B = (cs(W+1:end) - cs(1:end-W)) ./ W
--
   "No one has the right to destroy another person's belief by
   demanding empirical evidence." -- Ann Landers

Subject: average problem

From: Randy Poe

Date: 26 Mar, 2008 18:57:13

Message: 8 of 10

On Mar 26, 2:26 pm, "Adam Bright" <abc_services_2...@yahoo.co.uk>
wrote:
> Hi there, this may seem really trivial but i seem to be
> stuck to perform the follwing command for my array.
>
> how do i say to provide an average value between every 7 values?
>
> e.g in excel I have this:
> =AVERAGE(I1:I8)
> =AVERAGE(I2:I9)
> =AVERAGE(I3:I10)
> ...
>
> Thanks for your help!

One of many solutions:

Assume x contains the data for which you want
the moving average in a row vector.

y = conv(x, ones(1, 8)/8);
y = y(8:end-7);

              - Randy

Subject: average problem

From: Adam Bright

Date: 26 Mar, 2008 19:08:01

Message: 9 of 10

Hi walter I'm having problems using your code when replacing
(a) with my array, i get this:

??? Error using ==> horzcat
CAT arguments dimensions are not consistent.

Error in ==> semester2easter at 43
cs = [0 cumsum(values)];


roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in
message <fse66s$ob3$1@canopus.cc.umanitoba.ca>...
> In article <fse59u$n1r$1@canopus.cc.umanitoba.ca>,
> Walter Roberson <roberson@ibd.nrc-cnrc.gc.ca> wrote:
> >In article <fse4fq$gsq$1@fred.mathworks.com>,
> >Adam Bright <abc_services_2000@yahoo.co.uk> wrote:
>
> >>how do i say to provide an average value between every 7
values?
>
> >Is the vector an exact multiple of 7 long? If so then reshape
> >it to be 7 x whatever, and take the mean() along the
first dimension.
>
> Ah, that answer was for each -group- of 7 values. What you
want
> is more commonly called a "moving average".
>
> W = 7;
> cs = [0 cumsum(A)];
> B = (cs(W+1:end) - cs(1:end-W)) ./ W
> --
> "No one has the right to destroy another person's belief by
> demanding empirical evidence." -- Ann
Landers

Subject: average problem

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 26 Mar, 2008 19:12:26

Message: 10 of 10

In article <fse6uh$qnf$1@fred.mathworks.com>,
Adam Bright <abc_services_2000@yahoo.co.uk> wrote:
>Hi walter I'm having problems using your code when replacing
>(a) with my array, i get this:

>??? Error using ==> horzcat
>CAT arguments dimensions are not consistent.

>Error in ==> semester2easter at 43
>cs = [0 cumsum(values)];

cs = [0; cumsum(values(:))];

should work regardless of whether values is a row vector or
a column vector.
--
  "The shallow murmur, but the deep are dumb." -- Sir Walter Raleigh

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