How to calculate the mean of an interval (from a vector) in a loop?

3 次查看(过去 30 天)
Hey,
I would like to calculate the mean in intervals of the vector total_sync_spk, but i dont know how to do it. My idea when doing it manually looks like this:
mean(total_sync_spk(1:10))
mean(total_sync_spk(11:20)) and so on ...
This is my script:
all_means = [];
all_stds = [];
total_sync_spk = [];
for Gsyn = 0:2:20
for i = 1:10
[spt1, spt2] = IFcoupling(300, 300, Vth1, Vth2, ...
Tref1, Tref2, Esyn1, Esyn2, Gsyn, Gsyn, stim_time, dt);
[Nco, xidx, yidx] = coincidenceCounterSophisticated(spt1,spt2,0.25);
total_sync_spk = [total_sync_spk Nco];
end
a_means = mean(total_sync_spk);
all_means = [all_means a_means];
a_stds = std(total_sync_spk);
all_stds = [all_stds a_stds];
end
  1 个评论
Mathieu NOE
Mathieu NOE 2021-1-27
hello Paul
first, you could probably index the all_means and all_stds (vectors) instead of doing concatenation, I mean in the outer loop :
all_means(Gsyn) = mean(total_sync_spk);
instead of the 2 lines
a_means = mean(total_sync_spk);
all_means = [all_means a_means];
then you can define an output for each individual segment like :
all_means1(Gsyn) = mean(total_sync_spk(1:10));
all_means2(Gsyn) = mean(total_sync_spk(11:20));
but this could be expanded in more generic way using another for loop (how many indexes ?)

请先登录,再进行评论。

回答(1 个)

Shashank Gupta
Shashank Gupta 2021-2-3
Hi Paul,
There is one more convenient way of doing this, you can reshape your array in a matrix of shape (rows,10) such that each column i contains total_sync_spk(i:i+10) and then call the mean function column wise and it will do your job. I can attach a small peice of code for your reference.
% Generate an array.
arr = 1:100';
% Reshape the array in (some_rows,10) format.
arr_reshape = reshape(arr,10,10);
% Take the mean column wise.
mean_arr_reshape = mean(arr_reshape,1);
I hope this helps.
Cheers.

类别

Help CenterFile Exchange 中查找有关 Statistics and Machine Learning Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by