How to plot a grid with a for loop.
조회 수: 3 (최근 30일)
이전 댓글 표시
Hi everyone,
I am trying to design a program which must be able to plot a grid (of dost for example in this specific case).
So the user enters the value n of the number of dots that the grid must have (I have entered here 46 as an example) and now the program must draw all the dots beggining with one knew dot (the dot 0).
From the coordinates of that knew dot, the following ones (until the n-th one, that is, until the 46-th one) must be drawn by adding 1 to the X coordinate of the previous one and adding 3 to the Y coordinate of the previous one.
I have tried to design that program in the following code (with a for loop) but it doesn`t work, it seems that matlab doesn't recognize that notation that I have written in the 7-th and 8-th lines of code..
dot(0)=[6,6]; %The first dot of all (knew)
i=0;
j=0;
for 1:46 %I want to draw up to n dots (46 for example)
i=i+1;
j=i-1;
dot(i)(1)=dot(j)(1)+1; %The i-th dot's X coordinate is the previous dot's X coordinate +1
dot(i)(2)=dot(j)(2)+3; %The i-th dot's Y coordinate is the previous dot's Y coordinate +3
end
Anyone knows how program it in the correct way?
Anyone knows the correct way of writing those 7-th and 8-th line of code?
Thanks very much to everyone.
댓글 수: 2
Image Analyst
2021년 7월 17일
Are you trying to create a digital image (an array), that you'd display with imshow()? Or are you trying to create a graph where you'd plot dots with a command like plot(x, y, 'b.', 'MarkerSize', 50)?
채택된 답변
Yazan
2021년 7월 17일
N = 64;
a = 7;
b = 9;
x = a:a+N-1;
y = b:3:b+3*N-1;
figure,
plot(x, y, 'o'), grid minor
xlabel('X'), ylabel('Y');
댓글 수: 5
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Graphics Object Properties에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!