2 Variable increment in one "for Loop'
Show older comments
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.
Answers (2)
for i = 1:10
for j = 1:2:20
[i j]
end
end
5 Comments
Anuj Nandal
on 15 Dec 2020
KSSV
on 15 Dec 2020
In C also the give code increments as shown in MATLAB.
Bjorn Gustavsson
on 15 Dec 2020
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
on 15 Dec 2020
Edited: KSSV
on 15 Dec 2020
KSSV
on 15 Dec 2020
Thats fine.
SHIVAM KUMAR
on 15 Dec 2020
Or use j= 2*i
for i=1:9 %the end term will also come so use 9 here.
j=2*i;
%function
end
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!