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:
using the mean.m function

Subject: using the mean.m function

From: Glauston Lima

Date: 22 Oct, 2010 04:23:03

Message: 1 of 4

My friends,

suppose I have the matrix A=
                                          [1 4 6 7 8
                                           5 0 2 3 3
                                           9 4 8 5 2
                                           1 0 7 2 1]
                                           
and suppose I need to calculate (separately) the average of the columns (1, 2, 4) and the average of the columns (3, 5). Namely, I want to get the average of matrices:

[1 4 7 [6 8
 5 0 3 2 3
 9 4 5 8 2
 1 0 2] 7 1]
 
My question is as follows:

is there any Matlab command that returns these averages all at once?

Please understand: I'd like to know if it is possible to calculate the averages using a single Matlab command, without the need of loops .

Thank you in advance for your attention

Glauston

Subject: using the mean.m function

From: Miroslav Balda

Date: 22 Oct, 2010 07:15:04

Message: 2 of 4

"Glauston Lima" <glau11@gmail.com> wrote in message <i9r3j7$e8o$1@fred.mathworks.com>...
> My friends,
>
> suppose I have the matrix A=
> [1 4 6 7 8
> 5 0 2 3 3
> 9 4 8 5 2
> 1 0 7 2 1]
>
> and suppose I need to calculate (separately) the average of the columns (1, 2, 4) and the average of the columns (3, 5). Namely, I want to get the average of matrices:
>
> [1 4 7 [6 8
> 5 0 3 2 3
> 9 4 5 8 2
> 1 0 2] 7 1]
>
> My question is as follows:
>
> is there any Matlab command that returns these averages all at once?
>
> Please understand: I'd like to know if it is possible to calculate the averages using a single Matlab command, without the need of loops .
>
> Thank you in advance for your attention
>
> Glauston

Hi Glaustone,
if you need an average (not averages) you have to do it in two steps in your problem:
m124 = mean(A(:,1);A(:,2);A(:,4));
m35 = mean(A(:,3);A(:,5));
Is it OK?
In general case, you put the indeces of columns in the vector ix, say ix=(1,2,4),
make a new matrix of selected columns As = A(,:ix) and calculate mean(As(:));
It is all.
Mira

Subject: using the mean.m function

From: Cris Luengo

Date: 22 Oct, 2010 13:04:03

Message: 3 of 4

"Miroslav Balda" <miroslav.nospam@balda.cz> wrote in message <i9rdlo$bni$1@fred.mathworks.com>...
> "Glauston Lima" <glau11@gmail.com> wrote in message <i9r3j7$e8o$1@fred.mathworks.com>...
> > My friends,
> >
> > suppose I have the matrix A=
> > [1 4 6 7 8
> > 5 0 2 3 3
> > 9 4 8 5 2
> > 1 0 7 2 1]
> >
> > and suppose I need to calculate (separately) the average of the columns (1, 2, 4) and the average of the columns (3, 5). Namely, I want to get the average of matrices:
> >
> > [1 4 7 [6 8
> > 5 0 3 2 3
> > 9 4 5 8 2
> > 1 0 2] 7 1]
> >
> > My question is as follows:
> >
> > is there any Matlab command that returns these averages all at once?
> >
> > Please understand: I'd like to know if it is possible to calculate the averages using a single Matlab command, without the need of loops .
> >
> > Thank you in advance for your attention
> >
> > Glauston
>
> Hi Glaustone,
> if you need an average (not averages) you have to do it in two steps in your problem:
> m124 = mean(A(:,1);A(:,2);A(:,4));
> m35 = mean(A(:,3);A(:,5));
> Is it OK?
> In general case, you put the indeces of columns in the vector ix, say ix=(1,2,4),
> make a new matrix of selected columns As = A(,:ix) and calculate mean(As(:));
> It is all.
> Mira

Another way is to compute the average of all columns first:
   m = mean(A);
and then average together the columns you need:
   [mean(m([1,2,4)),mean(m([3,5]))]

Cheers,
Cris.

Subject: using the mean.m function

From: Miroslav Balda

Date: 22 Oct, 2010 13:26:03

Message: 4 of 4

"Cris Luengo" <cris.luengo@google.for.my.name.to.contact.me> wrote in message <i9s243$dj8$1@fred.mathworks.com>...
> "Miroslav Balda" <miroslav.nospam@balda.cz> wrote in message <i9rdlo$bni$1@fred.mathworks.com>...
> > "Glauston Lima" <glau11@gmail.com> wrote in message <i9r3j7$e8o$1@fred.mathworks.com>...
> > > My friends,
> > >
> > > suppose I have the matrix A=
> > > [1 4 6 7 8
> > > 5 0 2 3 3
> > > 9 4 8 5 2
> > > 1 0 7 2 1]
> > >
> > > and suppose I need to calculate (separately) the average of the columns (1, 2, 4) and the average of the columns (3, 5). Namely, I want to get the average of matrices:
> > >
> > > [1 4 7 [6 8
> > > 5 0 3 2 3
> > > 9 4 5 8 2
> > > 1 0 2] 7 1]
> > >
> > > My question is as follows:
> > >
> > > is there any Matlab command that returns these averages all at once?
> > >
> > > Please understand: I'd like to know if it is possible to calculate the averages using a single Matlab command, without the need of loops .
> > >
> > > Thank you in advance for your attention
> > >
> > > Glauston
> >
> > Hi Glaustone,
> > if you need an average (not averages) you have to do it in two steps in your problem:
> > m124 = mean(A(:,1);A(:,2);A(:,4));
> > m35 = mean(A(:,3);A(:,5));
> > Is it OK?
> > In general case, you put the indeces of columns in the vector ix, say ix=(1,2,4),
> > make a new matrix of selected columns As = A(,:ix) and calculate mean(As(:));
> > It is all.
> > Mira
>
> Another way is to compute the average of all columns first:
> m = mean(A);
> and then average together the columns you need:
> [mean(m([1,2,4)),mean(m([3,5]))]
>
> Cheers,
> Cris.


Or by the one-liner
means = mean(A(:,[1,2,4,3,5]))
Mira

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