Relationship between (i & j) of a nested for loop and (x & y) coordinates?
17 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Amanda Liu
am 22 Jun. 2021
Kommentiert: Amanda Liu
am 22 Jun. 2021
If I have a nested for loop:
T=ones(n)
for i = 1 : nx
for j = 1 : ny
T(i,j) = %something
end
end
I understand i goes down and j goes right in a loop but isn't it supposed to be the opposite where i is the horizontal row nd j is the vertical column?
for i = 1 : nx
for j = 1 : ny
Does this mean that the T(i,j) I'm plotting is in the direction of:
I'm a little confused about (i&j) and (x&y)..
0 Kommentare
Akzeptierte Antwort
KSSV
am 22 Jun. 2021
It moves from bottom to top and left to right. Check with below:
nx = 10 ;
ny = 10 ;
figure
hold on
for i = 1:nx
for j = 1:ny
plot(i,j,'*')
drawnow
text(i,j,num2str([i j]))
end
end
Why you are confused with direction? How does it matter?
1 Kommentar
Weitere Antworten (1)
Dhruv G
am 22 Jun. 2021
As @KSSV has noted, the for loop just means that the indices will increase while the loop runs. In a table, what you've drawn is correct. But in a right-handed coordinate plane ( the normal axes that we're used to), the indices increase when moving right and moving upward. The indices of the for loop will obey this
Siehe auch
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!