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

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com