Effacer les filtres
Effacer les filtres

How to plot a grid with a for loop.

11 vues (au cours des 30 derniers jours)
ErikJon Pérez Mardaras
ErikJon Pérez Mardaras le 17 Juil 2021
Modifié(e) : Yazan le 18 Juil 2021
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)
for 1:46 %I want to draw up to n dots (46 for example)
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
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 commentaires
Image Analyst
Image Analyst le 17 Juil 2021
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)?
ErikJon Pérez Mardaras
ErikJon Pérez Mardaras le 17 Juil 2021
Thanks for your reply!
What I am trying to do is the following.
In the real life you can write down in your notebook the following vectors following a pattern until the eternity.
And you can refer to its coordinates like that
I am trying to to the exactly SAME thing but in matlab, I mean, write (with a for loop for example) as many vectors as I wish following a simple pattern.
I know that a way of doing that is by writing you manually one by one like this:
%and so on manually....
But what I am trying to do is to generalize it, with a simple loop to save code lines and to be more quickly to do it by leting matlab to build those vectors in a quick moment instead of being you writing them one by one.

Connectez-vous pour commenter.

Réponse acceptée

Yazan le 17 Juil 2021
N = 64;
a = 7;
b = 9;
x = a:a+N-1;
y = b:3:b+3*N-1;
plot(x, y, 'o'), grid minor
xlabel('X'), ylabel('Y');
  5 commentaires
Yazan le 18 Juil 2021
Modifié(e) : Yazan le 18 Juil 2021
Save the data in a N-by-2 matrix. The first column is the X-coordinates and the second is the Y-coordinates.
dat = [x(:), y(:)];
Now, to access the datapoint of index i, just use
dot = dat(i, :);
ErikJon Pérez Mardaras
ErikJon Pérez Mardaras le 18 Juil 2021

Connectez-vous pour commenter.

Plus de réponses (0)


En savoir plus sur Line Plots dans Help Center et File Exchange




Community Treasure Hunt

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

Start Hunting!

Translated by