Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
How to take mean in 3rd dimension of matrix within a loop

Subject: How to take mean in 3rd dimension of matrix within a loop

From: Max

Date: 15 Mar, 2013 14:17:07

Message: 1 of 3

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

Subject: How to take mean in 3rd dimension of matrix within a loop

From: Matt J

Date: 15 Mar, 2013 15:13:06

Message: 2 of 3


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);
 

Subject: How to take mean in 3rd dimension of matrix within a loop

From: Steven_Lord

Date: 15 Mar, 2013 19:08:49

Message: 3 of 3



"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

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us