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,944
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
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
expect. See example 2 on this page:

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

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com




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

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.