how to run a loop according to two different index at the same time?

조회 수: 12 (최근 30일)
Zhixiao
Zhixiao 2014년 12월 25일
댓글: Trung Le 2022년 3월 3일
For example:
i=[0 1 3 6 10]
j=[1 3 6 10 15]
Normally, when we use
'for index1=i
for index2=i
....
end
end'
then Matlab will firstly set i=0 and then run all the vaules from 1 to 15 in index2, and then goes back to i=1.
However, in my problem, I require that i and j to be executed simultaneously, like: when i=0, j=1, then i=1, j=3.
Anyone can tell me how to do with it?
many thanks

채택된 답변

per isakson
per isakson 2014년 12월 25일
편집: per isakson 2014년 12월 25일
One way:
ii = [0,1,3,6,10];
jj = [1,3,6,10,15];
for ix = 1 : length(ii)
ii(ix)
jj(ix)
end
  댓글 수: 4
Peter Beringer
Peter Beringer 2020년 10월 30일
Brilliant answer! And so easily adaptable.

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

추가 답변 (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