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 commentaires

Rik
Rik le 13 Nov 2019
So you need the mean, but you don't want to change the number of elements. How do you want to solve that? Do you want the mean to be repeated?
BBB
BBB le 13 Nov 2019
Hi Rik,
Thanks for taking the time to respond.
No, I dont want the mean to be repeated. It has to be new 51x51 data set called lp which consists the average of the data set jk which is also 51x51 in dim.
Just wondering, what is the command for that though?
Thanks.
Adam
Adam le 13 Nov 2019
Modifié(e) : Adam le 13 Nov 2019
Your code appears to be set up to expect a scalar value as the mean rather than a 51x51 output since you declare lp as a vector (incidentally, using length is not a good idea here, use size(..., 1 ) instead).
You need to give a clearer indication of what it is you wantm because you talk about a 2d matrix, but have a 3d array initially, which you loop around to have a succession of 2d matrices. You then keep overwriting the result from these in lp, which presumably should instead be
lp(kk) = ...
Unless you want to do a moving mean over a window I don't understand how you expect the mean of some number of values to return a result of the same size.
Steven Lord
Steven Lord le 13 Nov 2019
Let's take a simpler and smaller example.
A = reshape(1:75, [5 5 3]);
jk = squeeze(A(1, :, :));
Now, for this jk (which is 5-by-3) what exactly do you want the value of lp to be when you take its mean? Show us numbers and explain how you calculated those numbers from this jk matrix. That may help us understand what you want to do in your larger problem with a random rand_image_sim array.
BBB
BBB le 13 Nov 2019
Hi Steven,
I am basically modelling some neuronal activity in primary visual cortex.
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.
Adam
Adam le 13 Nov 2019
Modifié(e) : Adam le 13 Nov 2019
Sounds like it means average over the 10,000 images?
Which would just be a
squeeze( mean( ... ) )
operation on your full set 3d set of results, i.e. the mean along dimension 1, your 10,000, leaving you with a 51x51 end result.
BBB
BBB le 13 Nov 2019
How would I need to change my code then?
Thanks
Adam
Adam le 13 Nov 2019
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.

Connectez-vous pour commenter.

Réponses (1)

Luna
Luna le 13 Nov 2019
Modifié(e) : Luna le 13 Nov 2019

0 votes

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 commentaires

BBB
BBB le 13 Nov 2019
Hi Luna,
Thanks for your response.
I tried this before, but it did not give me back what I wanted.
The z_cos and z_gauss are outputs of two other cos and gaussian functions. The only thing I want (which is the final step) to find the average of the jk data set which is a 51x51 dim in a 51x51 dim and then plot it with imagesc.
Luna
Luna le 13 Nov 2019
Modifié(e) : Luna le 13 Nov 2019
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 le 13 Nov 2019
Modifié(e) : BBB le 13 Nov 2019
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.

Connectez-vous pour commenter.

Catégories

Tags

Question posée :

BBB
le 13 Nov 2019

Commenté :

le 13 Nov 2019

Community Treasure Hunt

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

Start Hunting!

Translated by