Search All of the Math Forum:

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

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: How to take mean in 3rd dimension of matrix within a loop
Replies: 2   Last Post: Mar 15, 2013 3:08 PM

 Messages: [ Previous | Next ]
 Matt J Posts: 4,997 Registered: 11/28/09
Re: How to take mean in 3rd dimension of matrix within a loop
Posted: Mar 15, 2013 11:13 AM

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 Matt J
3/15/13 Steven Lord