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:
Averaging in 3rd dimension in a loop

Subject: Averaging in 3rd dimension in a loop

From: Max

Date: 15 Mar, 2013 14:00:08

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+4),3)];
end

Any help in this regard would highly appreciated. Thanks

Subject: Averaging in 3rd dimension in a loop

From: Jos (10584)

Date: 15 Mar, 2013 14:55:07

Message: 2 of 3

"Max" wrote in message <khv9h7$49f$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+4),3)];
> end
>
> Any help in this regard would highly appreciated. Thanks

You want to concatenate the averages in the third dimension (that is, making more planes in each loop):

A1 = cat(3,A1,mean(...,3))

[ x ; y ] concatenates x and y in the 1st dimension (making more rows)

~ Jos

Subject: Averaging in 3rd dimension in a loop

From: Matt J

Date: 15 Mar, 2013 15:13:06

Message: 3 of 3

"Max" wrote in message <khv9h7$49f$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+4),3)];
> end
>
> Any help in this regard would highly appreciated. Thanks
==============

Use the function below.

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

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