필터 지우기
필터 지우기

Running 2 counters in 1 for loop simultaneously

조회 수: 15 (최근 30일)
Wasim Ramadan
Wasim Ramadan 2020년 4월 28일
답변: Voss 2022년 12월 27일
I am trying to run a for loop that has 2 counters instead of one. Example
for i=1:2 j=0:12:12 ....
So, first iteration i=1 and j=0 second iteration i=2 and j=12
  댓글 수: 2
felimon mina
felimon mina 2022년 12월 27일
편집: felimon mina 2022년 12월 27일
because you write j = 0:12:12 so you use a step by 12 , so if you use step by 1 , j = 0:1:12 it runs better
Voss
Voss 2022년 12월 27일
@felimon mina: What if they want j = 0:12:12 and not j = 0:1:12? Surely, j = 0:1:12 would not be better if the desired behavior was j = 0:12:12, right?

댓글을 달려면 로그인하십시오.

채택된 답변

Voss
Voss 2022년 12월 27일
i_all = [1 2];
j_all = [0 12];
for idx = 1:numel(i_all)
i = i_all(idx);
j = j_all(idx);
% ...
end

추가 답변 (1개)

Deepak Gupta
Deepak Gupta 2020년 4월 28일
Hi,
you just need to define j outside the loop. And then use it with i as index.
j = 0:12:12;
for i= 1:2
% Write your code here using j with i as subscript.
%for example x(i) = j(i);
end

카테고리

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