Search All of the Math Forum:

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

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 ]
 Steven Lord Posts: 17,530 Registered: 12/7/04
Re: How to take mean in 3rd dimension of matrix within a loop
Posted: Mar 15, 2013 3:08 PM

"Max " <nizabat@gmail.com> wrote in message
news:khvah3\$7ov\$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+1),3)];
> end
>
> Any help in this regard would highly appreciated. Thanks

B = (A(:, :, 1:2:end) + A(:, :, 2:2:end)) / 2;

or if you want to avoid the ENDs for some reason:

nPages = size(A, 3);
B = (A(:, :, 1:2:nPages) + A(:, :, 2:2:nPages)) / 2;

Note that if A is 4-or-higher dimensional this will do something you may not

http://www.mathworks.com/help/matlab/ref/size.html

--
Steve Lord
slord@mathworks.com