# Calculate the time-averaged value every 10 iterations

10 visualizzazioni (ultimi 30 giorni)
m m il 11 Dic 2019
Commentato: m m il 11 Dic 2019
Hi,
I do the calculation of X (k) 1000x1 in a time loop for t = 1: 10000 (note that X does not have an iteration t) and I want to put a condition when t = 9000 to compute the averaged value (in the time) of X every 10 iterations ot t and when t> = 9000 : 10000
I want to get the size of X average like that 1000x10 without introducing a time iteration t in the variable X
is there a way to do that?
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposta accettata

Nicolas B. il 11 Dic 2019
Modificato: Nicolas B. il 11 Dic 2019
I'm trying to understand your question. I understand that every 10 iterations, you want to display the average computation time of the last 10 iterations. So I would like to recommend you to do something like that:
% create t to go faster
t = NaN(1, 10);
it = 1; % index in t array
for i = 1:10000
tic;
t(it) = toc; % get the execution time for this sample
% taking into account that i starts with 1
it = it + 1;
% display average time and reset counter
if it == 10
% compute and display average computation time
fprintf('My average computation time = %f\n', mean(t));
it = 1;
end
end
% display last iterations if not already done
if it ~= 1
fprintf('My average computation time = %f\n', mean(t(1:it)));
end
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
m m il 11 Dic 2019
my X(k) is calculated in this loop like this
for t =1:10000
Xaveraged = 0; %initialization of the averaged value
for k = 1:1000
X1(k) = ..
end
%here i need this condition
if t>=9000
Xaveraged = Xaveraaged + X1
end
end
Xaveraged = Xaveraged /10000
i want to get Xaveraged in differebt steps of time.
there is a way to get this??

Accedi per commentare.

### Categorie

Scopri di più su Boundary Conditions in Help Center e File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by