Create number of for loops depending on size of N
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
endHi, 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 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 for loop, nested for loop MATLAB Answers — New Questions