## calculating mean values of a 3D matrix

### noa (view profile)

on 9 Oct 2012
Latest activity Commented on by BBB

### BBB (view profile)

on 13 Nov 2019 at 12:23

### Andrei Bobrov (view profile)

hi.
I have a 3D matrix. size 79x63x100
I want to calc the mean of each of the 79x63 values so to get a 2D matrix the size of 79x63x1
but when i do mean(mat) i get it in the size of 1x63x100
how should I write it?
Thanks, Noa

Deepak Singh

### Deepak Singh (view profile)

on 15 Jul 2015
You can mention the dimension you want to take mean across. Something like this will work: x=mean(mat,3);

### Andrei Bobrov (view profile)

on 9 Oct 2012

out = mean(yourarray3D,3)

LarsFM

### LarsFM (view profile)

on 2 Mar 2018
I try to calclulate standard deviation with the same method:
out = std(yourarray3d,3);
But its not working in the same way as with the mean. What can I do to make it work?
Akshay Bhardwaj

### Akshay Bhardwaj (view profile)

on 24 May 2018
out = std(yourarray3d,[],3)
BBB

### BBB (view profile)

Hi,
I also have a similar question.
I have a 51x51 2D matrix that I need to take a mean off, but it has to give me back a 51x51 2D matrix as an output.
How can do this using the mean function. I need to put this in a loop as well. Please see code below for reference:
rand_image_stim = rand(10000,51,51);
kk = 1:length(rand_image_stim);
firing_rate_kk(kk) = 0;
gab_filt = z_cos.*z_gauss;
lp(kk) = 0;
for kk = 1:length(rand_image_stim)
output_kk = squeeze(rand_image_stim(kk,:,:)).*gab_filt;
firing_rate_kk(kk) = sum(output_kk,'all');
jk = squeeze(rand_image_stim(kk,:,:))*firing_rate_kk(kk);
lp = squeeze(mean(jk,2)); %Need help with this last bit!
end