Average number of trials not working

1 回表示 (過去 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.
  1 件のコメント
Ben Hatrick
Ben Hatrick 2022 年 1 月 17 日
Have since fixed!

サインインしてコメントする。

採用された回答

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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by