在正方形 (n x n) 内绘制等距点
Plot equal-spaced points within a square (n x n)
如何在 matlab 中绘制正方形等距点。如下图
. . . .
. . . .
. . . .
. . . .
下图是一个 4x4 尺寸的正方形。我想引用每个点并存储在变量 [Point(i).xcord, Point(i).ycord] 中并绘制如下所示:
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)
为了获得如上所示的网格形式的输出,谁能解释一个简单的方法。
您可以按如下方式使用ndgrid
:
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
是包含点坐标的矩阵。这比在您的代码中使用结构数组更快。您可以将它们编入索引,例如 xcord(2,3)
.
如果需要转成struct数组,使用
Point = struct('xcord', num2cell(xcord(:)), 'ycord', num2cell(ycord(:)));
如何在 matlab 中绘制正方形等距点。如下图
. . . .
. . . .
. . . .
. . . .
下图是一个 4x4 尺寸的正方形。我想引用每个点并存储在变量 [Point(i).xcord, Point(i).ycord] 中并绘制如下所示:
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)
为了获得如上所示的网格形式的输出,谁能解释一个简单的方法。
您可以按如下方式使用ndgrid
:
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
是包含点坐标的矩阵。这比在您的代码中使用结构数组更快。您可以将它们编入索引,例如 xcord(2,3)
.
如果需要转成struct数组,使用
Point = struct('xcord', num2cell(xcord(:)), 'ycord', num2cell(ycord(:)));