HOW CAN I EXECUTE TWO LOOPS "FOR" IN THE SAME TIME?

3 views (last 30 days)
[l,~]=size(barras);
for k=1:quant_barras
if k<=9 && SAIDA_FP(k,2)>=0
for i=1:l
if SAIDA_FP(k,1) >= barras(i,7) && SAIDA_FP(k,1) <= barras(i,8)
fprintf(arquivo,'O módulo da tensão da Barra %d está dentro da faixa pré-especificada. \r\n',k);
else
fprintf(arquivo,'O módulo da tensão da Barra %d NÃO está dentro da faixa pré-especificada. \r\n',k);
end
end
end
end
I need that k and i changes in the same time. Is it possible?
for k=1, i=1;
for k=2, i=2;
for k=3, i=3; (...)

Accepted Answer

Walter Roberson
Walter Roberson on 11 Jun 2018
No, it is not possible. Alternatives include:
1) assign the value of k to i as the first thing inside the loop
i = k;
2) Use the value of k to compute i
i = (quant_barras + 1 - k).^2 + 3; %i can be determined from k by formula
3) Use a single index to access lists of values of the two variables:
kvals = 1:quant_barras;
ivals = (quant_barras:-1:1).^2 + 3; %i has a corresponding value for each k, determined some way or other, perhaps passed in by a parameter
for kidx = 1 : length(kvals)
k = kvals(kidx);
i = ivals(kidx);
...
end
4) use an obscure facility of for loops to create a composite value that are column vectors
for ki = [1:quant_barras; (quant_barras:-1:1).^2 + 3] %the two lists of values as different rows
k = ki(1);
i = ki(2);
...
end
In this last obscure facility, each iteration of the "for" loop assigns the next column of the values to the loop control variable, and you can then break up the column into individual variables

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!