Average number of trials not working

2 vues (au cours des 30 derniers jours)
Ben Hatrick
Ben Hatrick le 17 Jan 2022
I am currentrly trying to find the average number of trials for all values in a given range to be outputted using a while function within a for loop. I want to run the while loop 1000 times and find the average number of iterations it takes to finish. As it stands I am only getting the number of iteratipons for the final trail as opposed to the average of the 1000. Any help would be very much appreciated. The code below may give further deatils.
n =2;
T = create_transition(n);
i = 1; % start at node 1 ( free choice)
trans_pdf = T(i,:); % get the PDF for node i
j = next_node(trans_pdf); % j now holds the destination node for this step in the simulation
n_trials = 10000;
i = 0;
n_iterations_needed = zeros(1,n_trials);
node_visit = j;
for p = 1:n_trials
while ~all(ismember(1:n^2,node_visit))
i = i+1;
node_visit(i) = j;
trans_pdf = T(j,:); % get the PDF for node i
j = next_node(trans_pdf); % j now holds the destination node for this step in the simulation
end
n_iterations_needed(p) = i;
end
n_avg = mean(n_iterations_needed)
The while loop works as expected on its own, but the for loop is not having the desired effect.
  1 commentaire
Ben Hatrick
Ben Hatrick le 17 Jan 2022
Have since fixed!

Connectez-vous pour commenter.

Réponse acceptée

Ben Hatrick
Ben Hatrick le 17 Jan 2022
for p = 1:n_trials
node_visit = [];
i =0;
while ~all(ismember(1:n^2,node_visit))
i = i+1;
node_visit(i) = j;
trans_pdf = T(j,:); % get the PDF for node i
j = next_node(trans_pdf); % j now holds the destination node for this step in the simulation
end
n_iterations_needed(p) = i;
end
n_avg = mean(n_iterations_needed)

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by