How to plot a grid with a for loop.
9 次查看(过去 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!