Average number of trials not working

조회 수: 2 (최근 30일)
Ben Hatrick
Ben Hatrick 2022년 1월 17일
답변: Ben Hatrick 2022년 1월 17일
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