How do I do a For Loop with two variables?

5 Ansichten (letzte 30 Tage)
Helena
Helena am 1 Jun. 2018
Kommentiert: Helena am 1 Jun. 2018
I want the loop to do for i=1:1:41 and for j=1:1:41, that is, the loop runs using i=1 and j=1, then i=2 and j=2 etc.
When I try the below nested function I get an infinite loop! for i=1:1:41 for j=1:1:41 Function end end
Can anyone help? Thanks!
  2 Kommentare
Stephen23
Stephen23 am 1 Jun. 2018
@Helena: if i always equals j, why use two variables?
Helena
Helena am 1 Jun. 2018
They're not the same, two different variables!

Melden Sie sich an, um zu kommentieren.

Antworten (1)

ES
ES am 1 Jun. 2018
Bearbeitet: ES am 1 Jun. 2018
Then you dont need two for loops.
for i=1:41
j=i;
....do something
...
end
By the way, i and j are poor choice of variable names!
  2 Kommentare
Helena
Helena am 1 Jun. 2018
Now I'm getting the error 'Subscript indices must either be real positive integers or logicals.'
Helena
Helena am 1 Jun. 2018
j is a date if that helps!

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by