Create number of for loops depending on size of N
Mostra commenti meno recenti
Hi, i have a question regarding number of nested loops:
In this case N would be 4 and hence there are 4 for loops
But if N = 2 in need 2 for loops and the formula also changes to i_1+1_2/N where N=2
is it possible to create code that creates the correct amount of for loops (corresponding to the value of N)
and also changes the formula for i_value in a correct way.
i_Max = 8
i_value = [];
i_real = [];
i_first = [];
i_second = [];
i_third = [];
i_forth = [];
tol = 0.01;
i_good = false;
while i_good == false
% generate new ratio's
for i_1 = 1:0.1:i_Max
for i_2 = 1:0.1:i_Max
for i_3 = 1:0.1:i_Max
for i_4 = 1:0.1:i_Max
i_value(end+1) = (i_1+i_2+i_3+i_4)/N;
if (i_average-tol <i_value(end)) && (i_value(end)<i_average+tol)...
&& (i_1>i_2) && (i_2>i_3) && (i_3>i_4)
i_good =true;
i_real(end+1) = i_value(end);
i_first(end+1) = i_1;
i_second(end+1) = i_2;
i_third(end+1) = i_3;
i_forth(end+1) = i_4;
end
end
end
end
end
end
Risposta accettata
Più risposte (1)
The N columns of the resulting C-matrix contain i_first, i_second,...
imax = 8;
N = 4;
i_average = (sum(0:N-1)/N+sum(imax:-1:imax-N+1)/N)/2;
tol = 0.5;
C = nchoosek(0:imax,N)
C = sort(C,2,'descend')
i_value = sum(C,2)/N
idx = abs(i_value-i_average)<tol
C = C(idx,:)
Categorie
Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!