How do I save value of variable in matrix form, which is overwritten after each iteration in for loop?
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Kiran Narwade
am 20 Jan. 2016
Kommentiert: Kiran Narwade
am 21 Jan. 2016
Here is the code that I have written, in this I want all the values of T in each iteration but it overwritten after each iteration and I am getting only last value of T.
load ('100m.mat') % the signal will be loaded to "val" matrix val = (val - 1024)/200; % you have to remove "base" and "gain" ECGsignal = val(1,1:3600); % select the lead (Lead I) Fs = 360; % sampling frequecy t = (0:length(ECGsignal)-1)/Fs; % time subplot(2,1,1) plot(t,ECGsignal) sig = smoothts(ECGsignal, 'b', 5); subplot(2,1,2) plot(t,sig)
%% Beat Rate Calculation beat_count=0; for k = 2 : length(sig)-1 if(sig(k)>sig(k-1) & sig(k)>sig(k+1) & sig(k)>0.5) k disp('Prominant Peak found') beat_count = beat_count+1; R = sig(k); % Value of R peak
T = (k/Fs) % Time at which R peak is occuring
disp('Time at which R peak is occuring(sec)')
end
end
N = length(sig);
Duration_in_seconds = N/Fs;
Duration_in_minutes = Duration_in_seconds/60;
BR = beat_count/Duration_in_minutes
0 Kommentare
Akzeptierte Antwort
goerk
am 20 Jan. 2016
Bearbeitet: goerk
am 20 Jan. 2016
You can save the values in a cell-array. As described in the following answer http://ch.mathworks.com/matlabcentral/answers/259579#answer_202813
Or if it is only a scalar value you can save the values in an array. eg. growing vector (slow)
T_vec = [];
for k=1:5
...
T_vec = [T_vec T];
end
eg. predefined(preallocated) vector (recommended because of performance)
T_vec = zeros(5,1);
for k= 2:6
...
T_vec(k-1) = T;
end
2 Kommentare
Stephen23
am 20 Jan. 2016
Growing a vector is not recommended as it makes the code slow. You should only use preallocated arrays, and never grow them in a loop:
Beginners typically grow their arrays in loops, then come here and complain that their code is slow. The solution is simple: preallocate arrays and do not grow them!
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Transmitters and Receivers 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!