Reputation: 210
How could I plot equally spaced points in square in matlab. As shown below
. . . .
. . . .
. . . .
. . . .
The figure below is for a 4x4 dimension square. I would like to reference each point and store in a variable [Point(i).xcord, Point(i).ycord] and plot as shown below:
For i=1:1:16
Point(i).xcord = <What expression goes here>
Point(i).ycord = <what expression goes here>
plot(Point(i).xcord, Point(i).ycord)
In order to get an output in grid form as shown above, could anyone explain a simple way of doing this.
Upvotes: 1
Views: 540
Reputation: 112699
You can use ndgrid
as follows:
N = 4; % Square size
[xcord, ycord] = ndgrid(1:N); % generate all combinations. Gives two matrices
plot(xcord(:), ycord(:), '.') % plot all points at once
axis([0 N+1 0 N+1]) % set axis limits
axis square % make actual sizes of both axes equal
xcord
, ycord
are matrices that contain the coordinates of the points. This is faster than using a struct array as in your code. You can index them such as xcord(2,3)
.
If you need to convert to a struct array, use
Point = struct('xcord', num2cell(xcord(:)), 'ycord', num2cell(ycord(:)));
Upvotes: 2