Average number of trials not working

2 次查看(过去 30 天)
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.

采纳的回答

Ben Hatrick
Ben Hatrick 2022-1-17
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)

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by