Drexel dragonThe Math ForumDonate to the Math Forum

Search All of the Math Forum:

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

Math Forum » Discussions » Software » comp.soft-sys.matlab

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

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Steven Lord

Posts: 17,945
Registered: 12/7/04
Re: How to take mean in 3rd dimension of matrix within a loop
Posted: Mar 15, 2013 3:08 PM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

"Max " <nizabat@gmail.com> wrote in message
> 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
expect. See example 2 on this page:


Steve Lord
To contact Technical Support use the Contact Us link on

Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© The Math Forum 1994-2015. All Rights Reserved.