HOW CAN I EXECUTE TWO LOOPS "FOR" IN THE SAME TIME?
2 ビュー (過去 30 日間)
古いコメントを表示
vinicius lanziotti
2018 年 6 月 11 日
回答済み: Walter Roberson
2018 年 6 月 11 日
[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; (...)
0 件のコメント
採用された回答
Walter Roberson
2018 年 6 月 11 日
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
0 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!