http://www.mathworks.com/matlabcentral/newsreader/view_thread/327585
MATLAB Central Newsreader  Averaging in 3rd dimension in a loop
Feed for thread: Averaging in 3rd dimension in a loop
enus
©19942015 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Fri, 15 Mar 2013 14:00:08 +0000
Averaging in 3rd dimension in a loop
http://www.mathworks.com/matlabcentral/newsreader/view_thread/327585#900115
Max
Dears, <br>
<br>
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); <br>
Now averaging (mean) over let say each 2 values in 3rd dimension, it should give me size 8x8x5. <br>
<br>
I did this, but doesn't work<br>
A1 = [];<br>
for m = 1: 2: 10<br>
A1 = [A1; mean(A(:,:,m:m+4),3)];<br>
end<br>
<br>
Any help in this regard would highly appreciated. Thanks

Fri, 15 Mar 2013 14:55:07 +0000
Re: Averaging in 3rd dimension in a loop
http://www.mathworks.com/matlabcentral/newsreader/view_thread/327585#900118
Jos (10584)
"Max" wrote in message <khv9h7$49f$1@newscl01ah.mathworks.com>...<br>
> Dears, <br>
> <br>
> 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); <br>
> Now averaging (mean) over let say each 2 values in 3rd dimension, it should give me size 8x8x5. <br>
> <br>
> I did this, but doesn't work<br>
> A1 = [];<br>
> for m = 1: 2: 10<br>
> A1 = [A1; mean(A(:,:,m:m+4),3)];<br>
> end<br>
> <br>
> Any help in this regard would highly appreciated. Thanks <br>
<br>
You want to concatenate the averages in the third dimension (that is, making more planes in each loop):<br>
<br>
A1 = cat(3,A1,mean(...,3)) <br>
<br>
[ x ; y ] concatenates x and y in the 1st dimension (making more rows)<br>
<br>
~ Jos

Fri, 15 Mar 2013 15:13:06 +0000
Re: Averaging in 3rd dimension in a loop
http://www.mathworks.com/matlabcentral/newsreader/view_thread/327585#900119
Matt J
"Max" wrote in message <khv9h7$49f$1@newscl01ah.mathworks.com>...<br>
> Dears, <br>
> <br>
> 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); <br>
> Now averaging (mean) over let say each 2 values in 3rd dimension, it should give me size 8x8x5. <br>
> <br>
> I did this, but doesn't work<br>
> A1 = [];<br>
> for m = 1: 2: 10<br>
> A1 = [A1; mean(A(:,:,m:m+4),3)];<br>
> end<br>
> <br>
> Any help in this regard would highly appreciated. Thanks <br>
==============<br>
<br>
Use the function below.<br>
<br>
A1=downsampn(A,[1,1,2]);<br>
<br>
<br>
function M=downsampn(M,bindims)<br>
%DOWNSAMPN  simple tool for downsampling ndimensional nonsparse arrays<br>
%<br>
% M=downsampn(M,bindims)<br>
%<br>
%in:<br>
%<br>
% M: an array<br>
% bindims: a vector of integer binning dimensions<br>
%<br>
%out:<br>
%<br>
% M: the downsized array<br>
<br>
nn=length(bindims);<br>
<br>
[sz{1:nn}]=size(M); %M is the original array<br>
sz=[sz{:}];<br>
<br>
<br>
newdims=sz./bindims;<br>
<br>
args=num2cell([bindims;newdims]);<br>
<br>
M=reshape(M,args{:});<br>
<br>
for ii=1:nn<br>
<br>
<br>
M=mean(M,2*ii1);<br>
<br>
end<br>
<br>
M=reshape(M,newdims);