Azzera filtri
Azzera filtri

How can I randomly excuted for loops inside MAIN FOR LOOP instead of run them in ordered ?

9 visualizzazioni (ultimi 30 giorni)
I want to select one of these for loops randmly to excute them NOT in ordered from loop 1 to loop 2 to Loop 3. for example in the first iteration of the main loop, Loop 3 is executed first and in the second iteration again selecting is randomly.
Thanks in advance
for q=1:100 % MAIN LOOP
for q1=1:10 % Loop 1
do some calculation
end
for q2=1:10 % Loop 2
do some calculation
end
for q3=1:10 % Loop 3
do some calculation
end
end

Risposta accettata

Aritra
Aritra il 31 Gen 2023
As per my understanding you are trying to execute the nested loops in random order inside the Main for Loop.
The order of execution of a for loop is linear. This behavior cannot be altered. However, you can use a randperm(n,k) function to generate a random number and the use the switch, case, otherwise expression in matlab to randomly execute a certain for loop.
To do so you can use a similar code snippet:
for q=1:5 % MAIN LOOP
t = randperm(3,1);
switch t
case 1
for q1=1:10 % Loop 1
disp(1);
end
case 2
for q2=1:10 % Loop 2
disp(2);
end
case 3
for q3=1:10 % Loop 3
disp(3);
end
end
end
For detail, please see this MathWorks documentation below for more information on randperm:
For detail, please see this MathWorks documentation below for more information on switch, case, otherwise:
  7 Commenti
Walter Roberson
Walter Roberson il 22 Feb 2023
randperm(6,1) is equivalent to randi(6,1,1)
If you want to choose a random order you can
t = randperm(3);
for q = 1 : length(t)
j = t(q);
switch j
stuff
end
end

Accedi per commentare.

Più risposte (3)

Jiri Hajek
Jiri Hajek il 31 Gen 2023
Hi, use randperm function to generate the random ordering.
growingOrder = 1:10;
randomOrder = growingOrder(randperm(length(growingOrder)));
For the random calling of loops, use switch-case command:
currentLoopIndex = randomOrder(q);
switch currentLoopIndex
case growingOrder(1)
% loop 1
case growingOrder(2)
% loop 2
case growingOrder(3)
% loop 3
end

Steven Lord
Steven Lord il 25 Feb 2023
Another possible solution to the problem, if you want to run a series of functions in a loop in a random order, is to use a cell array of function handles.
The line functionsToExecute{whichFunction}() extracts one of the three function handles from the cell array functionsToExecute then calls that function handle with no input arguments. The empty parentheses are necessary to call the function rather than simply displaying the function handle.
functionsToExecute = {@function1, @function2, @function3WithLoop};
for k = 1:5
orderOfFunctions = randperm(3);
for whichFunction = orderOfFunctions
fprintf("At iteration k = %d, running function %d\n", k, whichFunction)
functionsToExecute{whichFunction}()
end
fprintf("Outer loop iteration k = %d complete!\n \n", k)
end
At iteration k = 1, running function 1
Inside function 1
At iteration k = 1, running function 2
Function 2, executing!
At iteration k = 1, running function 3
function3WithLoop executing inner loop iteration k = 1 function3WithLoop executing inner loop iteration k = 2 function3WithLoop executing inner loop iteration k = 3
Outer loop iteration k = 1 complete!
At iteration k = 2, running function 2
Function 2, executing!
At iteration k = 2, running function 1
Inside function 1
At iteration k = 2, running function 3
function3WithLoop executing inner loop iteration k = 1 function3WithLoop executing inner loop iteration k = 2 function3WithLoop executing inner loop iteration k = 3
Outer loop iteration k = 2 complete!
At iteration k = 3, running function 2
Function 2, executing!
At iteration k = 3, running function 1
Inside function 1
At iteration k = 3, running function 3
function3WithLoop executing inner loop iteration k = 1 function3WithLoop executing inner loop iteration k = 2 function3WithLoop executing inner loop iteration k = 3
Outer loop iteration k = 3 complete!
At iteration k = 4, running function 2
Function 2, executing!
At iteration k = 4, running function 3
function3WithLoop executing inner loop iteration k = 1 function3WithLoop executing inner loop iteration k = 2 function3WithLoop executing inner loop iteration k = 3
At iteration k = 4, running function 1
Inside function 1
Outer loop iteration k = 4 complete!
At iteration k = 5, running function 2
Function 2, executing!
At iteration k = 5, running function 3
function3WithLoop executing inner loop iteration k = 1 function3WithLoop executing inner loop iteration k = 2 function3WithLoop executing inner loop iteration k = 3
At iteration k = 5, running function 1
Inside function 1
Outer loop iteration k = 5 complete!
function function1
disp("Inside function 1")
end
function function2
disp("Function 2, executing!")
end
function function3WithLoop
for k = 1:3
fprintf("function3WithLoop executing inner loop iteration k = %d\n", k)
end
end

Sarvesh Kale
Sarvesh Kale il 31 Gen 2023
Hi omar th,
I have borrowed a part of answer from Jiri Hajek, you can acheive the random execution of loop using following lines of code
% pre define a random generated array and call it select
select = randi([1,3],1,100); % this will create a random vector whose entries will be 1, 2, or 3
for q=1:100 % MAIN LOOP
switch select(q)
case 1
for q1=1:10 % Loop 1
%do some calculation
end
case 2
for q2=1:10 % Loop 2
%do some calculation
end
case 3
for q3=1:10 % Loop 3
%do some calculation
end
end
end
  3 Commenti
omar th
omar th il 20 Feb 2023
@Sarvesh Kale , When I run my code let's assume loop 2 is executed but I have a function in loop 3 and 1 are not executed. As illustrated in the code below, when I obtained the summuation of these functions, two of them is not executed thats why I got an error in " Sum_Total_Functions ". I appreciate any help
for q=1:5 % MAIN LOOP
t = randperm(3,1);
switch t
case 1
for q1=1:10 % Loop 1
disp(1);
Function_1(q1) = do some calculation
end
case 2
for q2=1:10 % Loop 2
disp(2);
Function_2(q2) = do some calculation
end
case 3
for q3=1:10 % Loop 3
disp(3);
Function_3(q3) = do some calculation
end
end
end
Sum_Total_Functions = Function_1+Function_2+Function_3;
Walter Roberson
Walter Roberson il 20 Feb 2023
Do not randperm each iteration separately. Your code t = randperm(3,1) is just a slower way (in context) of executing t = randi(3) : the next q iteration, you could potentially get exactly the same t value again.
I calculate, for example, that if you ask for 5 different single-value random numbers, that roughly 39.5% of the time, at least one of the functions will not get executed. The more iterations, the lower the probability that a function will be missed, but the probability will never go to 0 if at least one iteration is done.
If you want to execute each function a minimum of a certain number of times, first construct a control vector that contains as many copies of the function index as the minimum number of executions. For example,
[1,1,2,3]
for the case where function 1 must be executed at least twice and functions 2 and 3 must be executed at least once each. Then append to that randi() of the number of functions, as many times as needed to fill out the iterations, so that after this step, the control vector is the same length as the total number of iterations. Now take
order = ControlVector(randperm(numel(ControlVector)));
for q = 1 : number_of_iterations
t = order(q);
switch t
stuff
end
end
This arrangement permits you to have different minimum number of iterations for the different functions.
If you just need to ensure that each function is executed at least once,then you can do
ControlVector = [1:NumberOfFunctions, randi(NumberOfFunctions, 1, number_of_iterations - number_of_functions)];
order = ControlVector(randperm(numel(ControlVector)));

Accedi per commentare.

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Prodotti


Release

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by