Mean of a Matrix

조회 수: 3 (최근 30일)
BBB
BBB 2019년 11월 13일
댓글: Adam 2019년 11월 13일
Hi,
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.
Please see the sample code below:
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 = mean(jk,2); %this is where I need help
end
  댓글 수: 8
BBB
BBB 2019년 11월 13일
How would I need to change my code then?
Thanks
Adam
Adam 2019년 11월 13일
I don't really have the time to look in depth at the code and give a full syntactically correct answer, but from what I can see you just want to store jk in the loop as e.g.
jk( kk, :, : ) = squeeze(rand_image_stim(kk,:,:))*firing_rate_kk(kk);
and remove the last line from the loop, then after the end of the for loop simply:
result = mean( jk );
should give what you want.

댓글을 달려면 로그인하십시오.

답변 (1개)

Luna
Luna 2019년 11월 13일
편집: Luna 2019년 11월 13일
your for loop is length(rand_image_stim). So your mean value calculated 10000 times. You should store it and later get the mean all.
Try this:
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,51) = 0; % 10000 x 51 matrix
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(kk,:) = mean(jk,2); %this is where I need help
end
your_mean_value = mean(lp,'all');
  댓글 수: 3
Luna
Luna 2019년 11월 13일
편집: Luna 2019년 11월 13일
I have made a small edit to my answer please check again. You will have 10000 mean values for each loop. It is not possible to get 51x51 mean values. Or did I misunderstand? if you want to get 51x51 mean values in the end you should have kk = 51 but it is 10000. Also in the beginning you initialize lp 1x10000 vector filled with zeros already.
BBB
BBB 2019년 11월 13일
편집: BBB 2019년 11월 13일
This is my original question:
First generate 10,000 random images (i.e. where the brightness of each pixel is chosen randomly) of the same size as used in ex: 2.9(which is 51x51). For each of these stimuli, determine the firing rate (which is done by first getting the output in which you need to squeeze the rand_image_stim from 3D to 2D and then multiply with another 51x51 matrix which in this case is the gab_filt) of the cortical neuron. Then, multiply each of the stimuli with the respective firing rate (which is the jk variable here), and average the result (this is where I am getting stuck) and then plot with imagesc.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Descriptive Statistics and Visualization에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by