Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Averaging in 3rd dimension in a loop
Date: Fri, 15 Mar 2013 15:13:06 +0000 (UTC)
Organization: Xoran Technologies
Lines: 54
Message-ID: <khvdq2\$jes\$1@newscl01ah.mathworks.com>
References: <khv9h7\$49f\$1@newscl01ah.mathworks.com>
NNTP-Posting-Host: www-06-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: newscl01ah.mathworks.com 1363360386 19932 172.30.248.38 (15 Mar 2013 15:13:06 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Fri, 15 Mar 2013 15:13:06 +0000 (UTC)
Xref: news.mathworks.com comp.soft-sys.matlab:791243

"Max" wrote in message <khv9h7\$49f\$1@newscl01ah.mathworks.com>...
> Dears,
>
> I want to take average of long data array in 3rd dimension, but I am not getting the wanted result. Lets say I have A = rand(8,8,10);
> Now averaging (mean) over let say each 2 values in 3rd dimension, it should give me size 8x8x5.
>
> I did this, but doesn't work
> A1 = [];
> for m = 1: 2: 10
>     A1 = [A1; mean(A(:,:,m:m+4),3)];
> end
>
> Any help in this regard would highly appreciated. Thanks
==============

Use the function below.

A1=downsampn(A,[1,1,2]);

function M=downsampn(M,bindims)
%DOWNSAMPN - simple tool for downsampling n-dimensional nonsparse arrays
%
%  M=downsampn(M,bindims)
%
%in:
%
% M: an array
% bindims: a vector of integer binning dimensions
%
%out:
%
% M: the downsized array

nn=length(bindims);

[sz{1:nn}]=size(M); %M is the original array
sz=[sz{:}];

newdims=sz./bindims;

args=num2cell([bindims;newdims]);

M=reshape(M,args{:});

for ii=1:nn

M=mean(M,2*ii-1);

end

M=reshape(M,newdims);