Got Questions? Get Answers.
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:
Calculating Average of a matrix

Subject: Calculating Average of a matrix

From: Alan Leung

Date: 13 May, 2010 21:14:05

Message: 1 of 9

Hi,

Let say I have a matrix like this:

A = [1;2;3;4;5;6;7;8;9;10;11;12];

I would like to find out the average of every 6 elements such that it gives:

[3.5;9.5];

How can I do that?

Alan

Subject: Calculating Average of a matrix

From: TideMan

Date: 13 May, 2010 21:39:29

Message: 2 of 9

On May 14, 9:14 am, "Alan Leung" <alan.le...@rogers.com> wrote:
> Hi,
>
> Let say I have a matrix like this:
>
> A = [1;2;3;4;5;6;7;8;9;10;11;12];
>
> I would like to find out the average of every 6 elements such that it gives:
>
> [3.5;9.5];
>
> How can I do that?
>
> Alan

help reshape
help mean

Subject: Calculating Average of a matrix

From: tinne123

Date: 13 May, 2010 21:47:04

Message: 3 of 9

Hi,

fancy you made a typo in the supposed results, but maybe this is what you wanted:

I = [ mean(A(1:6)); mean(A(7:10))]
I =

    3.5000
    8.5000

Good luck!

Subject: Calculating Average of a matrix

From: Alan

Date: 13 May, 2010 23:16:20

Message: 4 of 9

Hi,

I was not clear in my previous message.

For A = [1;2;3;4;5;6;7;8;9;10;11;12]

B = [3.5;9.5;];

3.5 is the mean of 1,2,3,4,5,6.
9.5 is the mean of 7,8,9,10,11,12.

Let say I have a matrix that is 480000 x 1. I would like to have a matrix B such that B calculates the mean of every 6 elements (1~6,7~12,13~18,...,479995~480000). I am wondering how can I do that?

For the same situation, instead of mean let say I want to do Xth of percentile, how can I do that?

Alan






"tinne123 " <nastanova@yahoo.co.uk> wrote in message <hshs0o$amc$1@fred.mathworks.com>...
> Hi,
>
> fancy you made a typo in the supposed results, but maybe this is what you wanted:
>
> I = [ mean(A(1:6)); mean(A(7:10))]
> I =
>
> 3.5000
> 8.5000
>
> Good luck!

Subject: Calculating Average of a matrix

From: Walter Roberson

Date: 13 May, 2010 23:49:04

Message: 5 of 9

Alan wrote:

> Let say I have a matrix that is 480000 x 1. I would like to have a
> matrix B such that B calculates the mean of every 6 elements
> (1~6,7~12,13~18,...,479995~480000). I am wondering how can I do that?

The previous response pointing you to reshape was appropriate.

mean(reshape(B, 6, SomethingGoesHere))


> For the same situation, instead of mean let say I want to do Xth of
> percentile, how can I do that?

It is probably fairly much alike the code you would use for finding that
percentile for a vector, only applied to the reshaped matrix.

Subject: Calculating Average of a matrix

From: Walter Roberson

Date: 13 May, 2010 23:51:07

Message: 6 of 9

Alan wrote:

> Let say I have a matrix that is 480000 x 1. I would like to have a
> matrix B such that B calculates the mean of every 6 elements
> (1~6,7~12,13~18,...,479995~480000). I am wondering how can I do that?
>
> For the same situation, instead of mean let say I want to do Xth of
> percentile, how can I do that?

By the way: don't expect to be able to get very accurate percentiles -- you
will, after all, be taking the percentile over only 6 elements, so you average
16 2/3 percent gaps between the elements.

Subject: Calculating Average of a matrix

From: ImageAnalyst

Date: 14 May, 2010 00:11:44

Message: 7 of 9

Alan:
You can use conv() (it's optimized for speed so don't worry that it
calculates some values you don't need - it's blazingly fast)

% Generate sample data
A = [1:480000]';
N = 6; % per your requirement.
tic;
% Do the convolution
A_Smoothed = conv(A, ones(N, 1)/N);
% Pick out every 6th one.
B = A_Smoothed(N:N:(end-N));
toc;

Elapsed time is 0.028172 seconds.

B =
    3.5000
    9.5000
   15.5000
   21.5000
   27.5000
   33.5000
   39.5000
etc.

For the percentiles you need a "rank filter" (that's what you
described is called, at least in the image processing world), which is
not built in to any toolbox that I know of so I think you'd have to
use sort() and cumsum() along with something like nlfilter (image
processing toolbox).

Actually percentile is ambiguous. Do you mean like the 4th element in
size, regardless of what the actual sizes of the 6 elements are? Or
do you mean to calculate percentiles taking into account the value of
the 6 elements. For example, 3 would be the element at the 50th
percentile in this group [1,2,3,100 200 300] if you just go by ranking
them (it's the third biggest out of 6 elements total), but if you take
account the *values* of the elements, the 50th percentile if passed
only when you've reached the 200 element because (1+2+3+100+200) =
306, which is just past the 50% point of the total sum of
(1+2+3+100+200+300) = 606. So clarify what you mean.
-ImageAnalyst

Subject: Calculating Average of a matrix

From: Jan Simon

Date: 14 May, 2010 08:55:20

Message: 8 of 9

Dear Alan!

> A = [1;2;3;4;5;6;7;8;9;10;11;12];
> I would like to find out the average of every 6 elements such that it gives:
> [3.5;9.5];

Beside the already mentioned RESHAPE method, it is faster to calculate the MEAN manually with SUM and division:
  M = sum(reshape(A, 6, [])) / 6

I've created a C-Mex for calculating the mean of blocks also:
  http://www.mathworks.com/matlabcentral/fileexchange/24812

Good luck, Jan

Subject: Calculating Average of a matrix

From: tinne123

Date: 14 May, 2010 14:16:05

Message: 9 of 9

Hey, just disregard my post, from what you wrote I did not understand that you gave a concrete example for something more general.

"tinne123 " <nastanova@yahoo.co.uk> wrote in message <hshs0o$amc$1@fred.mathworks.com>...
> Hi,
>
> fancy you made a typo in the supposed results, but maybe this is what you wanted:
>
> I = [ mean(A(1:6)); mean(A(7:10))]
> I =
>
> 3.5000
> 8.5000
>
> Good luck!

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