Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.

Topic: Averaging in 3rd dimension in a loop
Replies: 2   Last Post: Mar 15, 2013 11:13 AM

 Messages: [ Previous | Next ]
 Matt J Posts: 4,979 Registered: 11/28/09
Re: Averaging in 3rd dimension in a loop
Posted: Mar 15, 2013 11:13 AM

"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);

Date Subject Author
3/15/13 Jos
3/15/13 Matt J