# How to plot a grid with a for loop.

21 views (last 30 days)

Show older comments

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 Comments

Image Analyst
on 17 Jul 2021

### Accepted Answer

Yazan
on 17 Jul 2021

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 Comments

### More Answers (0)

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!