Relationship between (i & j) of a nested for loop and (x & y) coordinates?

조회 수: 15 (최근 30일)
Amanda Liu
Amanda Liu 2021년 6월 22일
댓글: Amanda Liu 2021년 6월 22일
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)..

채택된 답변

KSSV
KSSV 2021년 6월 22일
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
Amanda Liu
Amanda Liu 2021년 6월 22일
Hmm..because if I were to assign boundary conditions at the sides of a square (left right top bottom)
should i code like this
T(:,1) = 2 % Left
T(:,end) = 3 % Right
T(1,:) = 1 % Top
T(end,:) = 4 % Bottom
or
T(:,1) = 4 % Left
T(:,end) = 1 % Right
T(1,:) = 2 % Top
T(end,:) = 3 % Bottom
but i do noticed that the top and bot should be reversed so that it plots in the correct direction
T(1,:) % Should be Bottom value
T(:,end) % Should be Top value

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

추가 답변 (1개)

Dhruv G
Dhruv G 2021년 6월 22일
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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by