필터 지우기
필터 지우기

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

조회 수: 2 (최근 30일)
vinicius lanziotti
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; (...)

채택된 답변

Walter Roberson
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개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by