Average number of trials not working

2 Ansichten (letzte 30 Tage)
Ben Hatrick
Ben Hatrick am 17 Jan. 2022
Beantwortet: Ben Hatrick am 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.

Akzeptierte Antwort

Ben Hatrick
Ben Hatrick am 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)

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements 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