```Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: obatining mean value for every fixed numbers of elements
Date: Mon, 15 Dec 2008 06:26:01 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 26
Message-ID: <gi4t9p\$dm9\$1@fred.mathworks.com>
References: <gi2ksb\$qsa\$1@fred.mathworks.com>
NNTP-Posting-Host: webapp-05-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1229322361 14025 172.30.248.35 (15 Dec 2008 06:26:01 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Mon, 15 Dec 2008 06:26:01 +0000 (UTC)
Xref: news.mathworks.com comp.soft-sys.matlab:506991

"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

```