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:
Calc average of Matrix

Subject: Calc average of Matrix

From: John Stane

Date: 4 Nov, 2010 15:59:04

Message: 1 of 19

Hi,

I'm having hard time figuring this matrix out, and would like help.

A = [2;4;6;8;10;13;15]

I'm trying to find out the average of 2 elements, but still reuse the next element..

So it gives,

[3;5;7;9;11.5;14]

it calculate like this..
2+4 =6 /2 =3
4+6 = 10 / 2 = 5
6+8 = 14 / 2 = 7
8+10 = 18 / 2 = 9
10+13 = 23 / 2 = 11.5
13+15 = 28 / 2/ = 14

So far I got this code, which helps but still stuck..

M = sum(reshape(A,2,[]))/2

That code does the average of every 2 elements and does not reuse the element.

Subject: Calc average of Matrix

From: Sean

Date: 4 Nov, 2010 16:11:04

Message: 2 of 19

"John Stane" <starface245@hotmail.com> wrote in message <iaul88$p76$1@fred.mathworks.com>...
> Hi,
>
> I'm having hard time figuring this matrix out, and would like help.
>
> A = [2;4;6;8;10;13;15]
>
> I'm trying to find out the average of 2 elements, but still reuse the next element..
>
> So it gives,
>
> [3;5;7;9;11.5;14]
>
> it calculate like this..
> 2+4 =6 /2 =3
> 4+6 = 10 / 2 = 5
> 6+8 = 14 / 2 = 7
> 8+10 = 18 / 2 = 9
> 10+13 = 23 / 2 = 11.5
> 13+15 = 28 / 2/ = 14
>
> So far I got this code, which helps but still stuck..
>
> M = sum(reshape(A,2,[]))/2
>
> That code does the average of every 2 elements and does not reuse the element.

mean([A(1:end-1), A(2:end)],2)

Subject: Calc average of Matrix

From: Jan Simon

Date: 4 Nov, 2010 17:06:04

Message: 3 of 19

Dear John,

  A = [2;4;6;8;10;13;15]
  B = (A(1:end - 1) + A(2:end)) * 0.5

Jan

Subject: Calc average of Matrix

From: John Stane

Date: 4 Nov, 2010 18:08:04

Message: 4 of 19

^^ thanks both examples works.

Lets say k = x number of elements in a matrix and is it divided by k.
The example I gave that k = 2. What if k=5 or k=9 , etc....

How would that work?

Subject: Calc average of Matrix

From: John Stane

Date: 4 Nov, 2010 18:12:04

Message: 5 of 19

"John Stane" <starface245@hotmail.com> wrote in message <iausq4$jhi$1@fred.mathworks.com>...
> ^^ thanks both examples works.
>
> Lets say k = x number of elements in a matrix and is it divided by k.
> The example I gave that k = 2. What if k=5 or k=9 , etc....
>
> How would that work?


What if k=5 or k=9 , etc....
Given if there are more elements in the matrix.

Subject: Calc average of Matrix

From: Sean

Date: 4 Nov, 2010 18:23:04

Message: 6 of 19

"John Stane" <starface245@hotmail.com> wrote in message <iaut1k$5q1$1@fred.mathworks.com>...
> "John Stane" <starface245@hotmail.com> wrote in message <iausq4$jhi$1@fred.mathworks.com>...
> > ^^ thanks both examples works.
> >
> > Lets say k = x number of elements in a matrix and is it divided by k.
> > The example I gave that k = 2. What if k=5 or k=9 , etc....
> >
> > How would that work?
>
>
> What if k=5 or k=9 , etc....
> Given if there are more elements in the matrix.

A = [2;4;6;8;10;13;15];
k = 3;
conv(A,ones(k,1),'valid')/k

Subject: Calc average of Matrix

From: John Stane

Date: 4 Nov, 2010 18:50:05

Message: 7 of 19

"Sean " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <iautm8$hms$1@fred.mathworks.com>...
> "John Stane" <starface245@hotmail.com> wrote in message <iaut1k$5q1$1@fred.mathworks.com>...
> > "John Stane" <starface245@hotmail.com> wrote in message <iausq4$jhi$1@fred.mathworks.com>...
> > > ^^ thanks both examples works.
> > >
> > > Lets say k = x number of elements in a matrix and is it divided by k.
> > > The example I gave that k = 2. What if k=5 or k=9 , etc....
> > >
> > > How would that work?
> >
> >
> > What if k=5 or k=9 , etc....
> > Given if there are more elements in the matrix.
>
> A = [2;4;6;8;10;13;15];
> k = 3;
> conv(A,ones(k,1),'valid')/k

That code does not seem to work for me, it was giving bunch of errors.

Subject: Calc average of Matrix

From: Sean

Date: 4 Nov, 2010 19:08:04

Message: 8 of 19


> > A = [2;4;6;8;10;13;15];
> > k = 3;
> > conv(A,ones(k,1),'valid')/k
>
> That code does not seem to work for me, it was giving bunch of errors.

Well what errors? It works fine for me. Post the entire error message.

Subject: Calc average of Matrix

From: John Stane

Date: 4 Nov, 2010 19:33:04

Message: 9 of 19

"Sean " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <iav0ak$djl$1@fred.mathworks.com>...
>
> > > A = [2;4;6;8;10;13;15];
> > > k = 3;
> > > conv(A,ones(k,1),'valid')/k
> >
> > That code does not seem to work for me, it was giving bunch of errors.
>
> Well what errors? It works fine for me. Post the entire error message.

Invalid call to conv. Correct usage is:
--Function File: conv (A,B)

error: called from 'conv' in file (shows path)
error: evaluating binary operator '/'
error: evaluating assignment expression

Subject: Calc average of Matrix

From: Sean

Date: 4 Nov, 2010 20:29:05

Message: 10 of 19

"John Stane" <starface245@hotmail.com> wrote in message <iav1pg$km7$1@fred.mathworks.com>...
> "Sean " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <iav0ak$djl$1@fred.mathworks.com>...
> >
> > > > A = [2;4;6;8;10;13;15];
> > > > k = 3;
> > > > conv(A,ones(k,1),'valid')/k
> > >
> > > That code does not seem to work for me, it was giving bunch of errors.
> >
> > Well what errors? It works fine for me. Post the entire error message.
>
> Invalid call to conv. Correct usage is:
> --Function File: conv (A,B)
>
> error: called from 'conv' in file (shows path)
> error: evaluating binary operator '/'
> error: evaluating assignment expression


You must be using an old version of ML.

Subject: Calc average of Matrix

From: Walter Roberson

Date: 4 Nov, 2010 20:40:14

Message: 11 of 19

On 10-11-04 03:29 PM, Sean wrote:
> "John Stane" <starface245@hotmail.com> wrote in message
> <iav1pg$km7$1@fred.mathworks.com>...
>> "Sean " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message
>> <iav0ak$djl$1@fred.mathworks.com>...
>> > > > > A = [2;4;6;8;10;13;15];
>> > > > k = 3;
>> > > > conv(A,ones(k,1),'valid')/k
>> > > > > That code does not seem to work for me, it was giving bunch of
>> errors.
>> > > Well what errors? It works fine for me. Post the entire error
>> message.
>>
>> Invalid call to conv. Correct usage is:
>> --Function File: conv (A,B)
>>
>> error: called from 'conv' in file (shows path)
>> error: evaluating binary operator '/'
>> error: evaluating assignment expression
>
>
> You must be using an old version of ML.

It must be *very* old if the error messages are like that. No reasonably
current version would continue on to mention that the '/' binary operator was
being evaluated, as in all current versions the evaluation of the '/' binary
operator would not be considered to start until after the conv() call had
completed.

When I read the error messages, my thought was that perhaps the original
poster is using Simulink.

Subject: Calc average of Matrix

From: dpb

Date: 4 Nov, 2010 21:52:42

Message: 12 of 19

Walter Roberson wrote:
...

> It must be *very* old if the error messages are like that. No reasonably
> current version would continue on to mention that the '/' binary
> operator was being evaluated, as in all current versions the evaluation
> of the '/' binary operator would not be considered to start until after
> the conv() call had completed.
>
> When I read the error messages, my thought was that perhaps the original
> poster is using Simulink.

Octave, maybe???

R12 just says

?? Error using ==> conv
Too many input arguments.

--

Subject: Calc average of Matrix

From: John Stane

Date: 4 Nov, 2010 22:02:04

Message: 13 of 19

dpb <none@non.net> wrote in message <iava7s$5dh$3@news.eternal-september.org>...
> Walter Roberson wrote:
> ...
>
> > It must be *very* old if the error messages are like that. No reasonably
> > current version would continue on to mention that the '/' binary
> > operator was being evaluated, as in all current versions the evaluation
> > of the '/' binary operator would not be considered to start until after
> > the conv() call had completed.
> >
> > When I read the error messages, my thought was that perhaps the original
> > poster is using Simulink.
>
> Octave, maybe???
>
> R12 just says
>
> ?? Error using ==> conv
> Too many input arguments.
>
> --

Correct, I'm using Octave...

Is there another method or algorithm beside Sean just provided using conv.?

Subject: Calc average of Matrix

From: Walter Roberson

Date: 4 Nov, 2010 22:24:55

Message: 14 of 19

On 10-11-04 05:02 PM, John Stane wrote:

> Correct, I'm using Octave...

I'm not enthralled with trying to predict what Octave does or does not
implement. There are Octave resources for people who choose to work in Octave.

> Is there another method or algorithm beside Sean just provided using conv.?

cumsum() the original array. Calculate the cumulative sum indexed at k+1:end,
minus the cumulative sum indexed at 1:end-k, and divide the resulting vector by k.

Note: the cumsum() approach will not work if you have a mix of values that
vary considerably in magnitude and it is important to preserve the relative
precisions of adjacent elements.

Subject: Calc average of Matrix

From: John Stane

Date: 5 Nov, 2010 14:25:05

Message: 15 of 19

Walter Roberson <roberson@hushmail.com> wrote in message <iavbrv$gdh$1@canopus.cc.umanitoba.ca>...
> On 10-11-04 05:02 PM, John Stane wrote:
>
> cumsum() the original array. Calculate the cumulative sum indexed at k+1:end,
> minus the cumulative sum indexed at 1:end-k, and divide the resulting vector by k.
>
> Note: the cumsum() approach will not work if you have a mix of values that
> vary considerably in magnitude and it is important to preserve the relative
> precisions of adjacent elements.

Could you write this in equation in matlab code for me? Thank you.

Subject: Calc average of Matrix

From: Sean

Date: 5 Nov, 2010 14:49:03

Message: 16 of 19

"John Stane" <starface245@hotmail.com> wrote in message <ib1441$t3a$1@fred.mathworks.com>...
> Walter Roberson <roberson@hushmail.com> wrote in message <iavbrv$gdh$1@canopus.cc.umanitoba.ca>...
> > On 10-11-04 05:02 PM, John Stane wrote:
> >
> > cumsum() the original array. Calculate the cumulative sum indexed at k+1:end,
> > minus the cumulative sum indexed at 1:end-k, and divide the resulting vector by k.
> >
> > Note: the cumsum() approach will not work if you have a mix of values that
> > vary considerably in magnitude and it is important to preserve the relative
> > precisions of adjacent elements.
>
> Could you write this in equation in matlab code for me? Thank you.

You have to zero-pad A with k-2 zeros:
%%
A = [0;2;4;6;8;10;13;15];
k = 3;

Csum = cumsum(A(1:end));

(Csum(k+1:end) - Csum(1:end-k))/3

Subject: Calc average of Matrix

From: Walter Roberson

Date: 5 Nov, 2010 15:36:23

Message: 17 of 19

On 05/11/10 9:25 AM, John Stane wrote:
> Walter Roberson <roberson@hushmail.com> wrote in message
> <iavbrv$gdh$1@canopus.cc.umanitoba.ca>...
>> On 10-11-04 05:02 PM, John Stane wrote:
>>
>> cumsum() the original array. Calculate the cumulative sum indexed at
>> k+1:end, minus the cumulative sum indexed at 1:end-k, and divide the
>> resulting vector by k.
>>
>> Note: the cumsum() approach will not work if you have a mix of values
>> that vary considerably in magnitude and it is important to preserve
>> the relative precisions of adjacent elements.
>
> Could you write this in equation in matlab code for me? Thank you.

Join the following into one line if it shows up as multiple lines to you:

lll = @(l11,l1l,ll1) subsref(l11,struct('type','()','subs',{l1l:ll1}));
ll = @(ll1,l1l)
(lll(ll1,l1l,length(ll1))-lll(ll1,1,length(ll1)-l1l+1))./l1l; B =
ll(cumsum(A),k);

Note by the way that this is *matlab* code; no attempt has been made to
make it Octave compatible.

Subject: Calc average of Matrix

From: John Stane

Date: 5 Nov, 2010 20:23:03

Message: 18 of 19

"Sean " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <ib15gv$2h5$1@fred.mathworks.com>...
> "John Stane" <starface245@hotmail.com> wrote in message <ib1441$t3a$1@fred.mathworks.com>...
> > Walter Roberson <roberson@hushmail.com> wrote in message <iavbrv$gdh$1@canopus.cc.umanitoba.ca>...
> > > On 10-11-04 05:02 PM, John Stane wrote:
> > >
> > > cumsum() the original array. Calculate the cumulative sum indexed at k+1:end,
> > > minus the cumulative sum indexed at 1:end-k, and divide the resulting vector by k.
> > >
> > > Note: the cumsum() approach will not work if you have a mix of values that
> > > vary considerably in magnitude and it is important to preserve the relative
> > > precisions of adjacent elements.
> >
> > Could you write this in equation in matlab code for me? Thank you.
>
> You have to zero-pad A with k-2 zeros:
> %%
> A = [0;2;4;6;8;10;13;15];
> k = 3;
>
> Csum = cumsum(A(1:end));
>
> (Csum(k+1:end) - Csum(1:end-k))/3

Thanks that equation works perfectly. I understand why you have to add the 0, but is there a way without doing the zero-pad A with k-2 zeros?

Subject: Calc average of Matrix

From: John Stane

Date: 5 Nov, 2010 20:24:03

Message: 19 of 19

Walter Roberson <roberson@hushmail.com> wrote in message <YvVAo.14304$F77.5722@newsfe07.iad>...
> On 05/11/10 9:25 AM, John Stane wrote:
> > Walter Roberson <roberson@hushmail.com> wrote in message
> > <iavbrv$gdh$1@canopus.cc.umanitoba.ca>...
> >> On 10-11-04 05:02 PM, John Stane wrote:
> >>
> >> cumsum() the original array. Calculate the cumulative sum indexed at
> >> k+1:end, minus the cumulative sum indexed at 1:end-k, and divide the
> >> resulting vector by k.
> >>
> >> Note: the cumsum() approach will not work if you have a mix of values
> >> that vary considerably in magnitude and it is important to preserve
> >> the relative precisions of adjacent elements.
> >
> > Could you write this in equation in matlab code for me? Thank you.
>
> Join the following into one line if it shows up as multiple lines to you:
>
> lll = @(l11,l1l,ll1) subsref(l11,struct('type','()','subs',{l1l:ll1}));
> ll = @(ll1,l1l)
> (lll(ll1,l1l,length(ll1))-lll(ll1,1,length(ll1)-l1l+1))./l1l; B =
> ll(cumsum(A),k);
>
> Note by the way that this is *matlab* code; no attempt has been made to
> make it Octave compatible.


You lost me in the equation :(

Tags for this Thread

No tags are associated with 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