2 Variable increment in one "for Loop'

I was using a code and i need to write one "for loop" with two variable incrementing simultaneously. There is easy code in C, C++, but i'm not able to find one for this one. For reference i want to do this in matlab:
for(i=1,j=2;i<10,j<20;i++,j+2)
I can't use nested loop , could someone please help with this.

답변 (2개)

KSSV
KSSV 2020년 12월 15일
편집: KSSV 2020년 12월 15일

0 개 추천

for i = 1:10
for j = 1:2:20
[i j]
end
end

댓글 수: 5

Anuj Nandal
Anuj Nandal 2020년 12월 15일
But in this nested case untill j increments are not finished i increments will not start. I wanted both to increment simultaneously, I'm actually new to matlab, please bear a little
KSSV
KSSV 2020년 12월 15일
In C also the give code increments as shown in MATLAB.
Bjorn Gustavsson
Bjorn Gustavsson 2020년 12월 15일
If you don't want nested loops then just assign the values you want j to have inside the i-loop. That is allowed.
Anuj Nandal
Anuj Nandal 2020년 12월 15일
편집: KSSV 2020년 12월 15일
Thanks for the help, but i think i got it:
j=2
if j<20
for i=1:10
% operation using i,j
j=j+2;
end
end
Is this right?
KSSV
KSSV 2020년 12월 15일
Thats fine.

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

SHIVAM KUMAR
SHIVAM KUMAR 2020년 12월 15일

0 개 추천

Or use j= 2*i
for i=1:9 %the end term will also come so use 9 here.
j=2*i;
%function
end

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

릴리스

R2020b

태그

질문:

2020년 12월 15일

댓글:

2020년 12월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by