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

조회 수: 8(최근 30일)
Paul Hinze
Paul Hinze 2021년 1월 27일
답변: Shashank Gupta 2021년 2월 3일
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(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];
a_means = mean(total_sync_spk);
all_means = [all_means a_means];
a_stds = std(total_sync_spk);
all_stds = [all_stds a_stds];
  댓글 수: 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 ?)

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


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.

Community Treasure Hunt

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

Start Hunting!

Translated by