Determine the number of "for" loops by the user
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
f4r3in
el 27 de Ag. de 2021
Comentada: f4r3in
el 30 de Ag. de 2021
Hi everyone
I want to write a code that takes the number of uncertain parameters from the user and creates a "for" loop according to their number and performs the calculations.
for example:
The user enters the number 4 as the number of uncertainty parameters and the code puts 4 "for" loops in a row. This way :
for i=1:I
for j=1:J
for k=1:K
for r=1:R
"calculations"
end
end
end
end
Is this possible in MatLab?
4 comentarios
KSSV
el 27 de Ag. de 2021
I think you can avoid using loops.....can you tell us what are those calculations?
I,J,K,R they will be always same?
Respuesta aceptada
Chunru
el 27 de Ag. de 2021
Editada: Chunru
el 27 de Ag. de 2021
% The number of iterations
ni = [3 4 2 2]; % 4 iterations [I J K R]; i is innerest loop
idx = ones(size(ni)); % first index
k = cumprod(ni);
for i=1:prod(ni)
% calculate index (ignore this part if calculation independent of index)
ii = i;
for j=length(ni):-1:2
ir = rem(ii-1, k(j-1)) + 1;
idx(j) = (ii - ir) /k(j-1) + 1;
ii = ir;
end
idx(1) = ii;
disp(idx)
% do calculation depending on idx
end
Más respuestas (0)
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!