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

2 Ansichten (letzte 30 Tage)
Paul Hinze
Paul Hinze am 27 Jan. 2021
Beantwortet: Shashank Gupta am 3 Feb. 2021
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 Kommentar
Mathieu NOE
Mathieu NOE am 27 Jan. 2021
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 ?)

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Shashank Gupta
Shashank Gupta am 3 Feb. 2021
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.

Kategorien

Mehr zu Just for fun finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by