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:
obatining mean value for every fixed numbers of elements

Subject: obatining mean value for every fixed numbers of elements

From: Yihui Hung

Date: 14 Dec, 2008 09:50:03

Message: 1 of 3

Hello,
I have a 6X4 data set and need to average them across every 3X2 data point. The example is as follows:
1, 2,3,4;
3, 4, 5,6;
7, 8, 9,10;
11, 12, 13, 14;
15, 16,17,18;
19,20,21 22;
I would like to obtain four means from1,3,7,2,4,8;3,5,9,4,6,10;11,15,19,12,16,20;13,17,21,14,18,22. Are there some useful matlab function to do this? Any suggestion will appreciated.

Sincerely,
Yihui

Subject: obatining mean value for every fixed numbers of elements

From: Johan Carlson

Date: 14 Dec, 2008 15:42:02

Message: 2 of 3

"Yihui Hung" <a489930026@yahoo.com.tw> wrote in message <gi2ksb$qsa$1@fred.mathworks.com>...
> Hello,
> I have a 6X4 data set and need to average them across every 3X2 data point. The example is as follows:
> 1, 2,3,4;
> 3, 4, 5,6;
> 7, 8, 9,10;
> 11, 12, 13, 14;
> 15, 16,17,18;
> 19,20,21 22;
> I would like to obtain four means from1,3,7,2,4,8;3,5,9,4,6,10;11,15,19,12,16,20;13,17,21,14,18,22. Are there some useful matlab function to do this? Any suggestion will appreciated.
>
> Sincerely,
> Yihui

Here's one way of doing it:

Create a 2D filter mask,
H = ones(3,2)/6;

Then filter your matrix (I called it X) with the "moving average"

Y = conv2(H,X);

You can now figure out which elements of Y are the ones you like and extract them.


/JC

Subject: obatining mean value for every fixed numbers of elements

From: Roger Stafford

Date: 15 Dec, 2008 06:26:01

Message: 3 of 3

"Yihui Hung" <a489930026@yahoo.com.tw> wrote in message <gi2ksb$qsa$1@fred.mathworks.com>...
> Hello,
> I have a 6X4 data set and need to average them across every 3X2 data point. The example is as follows:
> 1, 2,3,4;
> 3, 4, 5,6;
> 7, 8, 9,10;
> 11, 12, 13, 14;
> 15, 16,17,18;
> 19,20,21 22;
> I would like to obtain four means from1,3,7,2,4,8;3,5,9,4,6,10;11,15,19,12,16,20;13,17,21,14,18,22. Are there some useful matlab function to do this? Any suggestion will appreciated.
>
> Sincerely,
> Yihui

  Let A be your matrix and let the means be taken over p x q submatrices. Then execute the following (mysterious) code:

 [M,N] = size(A);
 m = M/p; n = N/q; % <-- m and n must evaluate to integers
 [J,I] = meshgrid(p*(0:m*n-1)+m*p*(q-1)*floor((0:m*n-1)/m),...
                 (1:p*q)+(m-1)*p*floor((0:p*q-1)/p));
 B = reshape(mean(reshape(A(I+J),p*q,m*n)),m,n);

Matrix B will be the requested m x n matrix of mean values.

Roger Stafford

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