Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 如何绘制NxN圆形阵列?_Arrays_Matlab - Fatal编程技术网

Arrays 如何绘制NxN圆形阵列?

Arrays 如何绘制NxN圆形阵列?,arrays,matlab,Arrays,Matlab,我想画一个NxN的圆数组。为了形象化,我附上了一张我想要实现的图像。我是MatlLab的新手,所以我尝试先绘制一个圆,下面是示例代码: n = 2^10; % size of mask M = zeros(n); I = 1:n; x = I - n/2; % mask x - coordinates y = n/2 - I; % mask y - coordinates [X,Y] = meshgr

我想画一个NxN的圆数组。为了形象化,我附上了一张我想要实现的图像。我是MatlLab的新手,所以我尝试先绘制一个圆,下面是示例代码:

n = 2^10;                   % size of mask
M = zeros(n);
I = 1:n;
x = I - n/2;                % mask x - coordinates
y = n/2 - I;                % mask y - coordinates
[X,Y] = meshgrid(x,y);      % create 2-D mask grid
R = 200;                     % aperture radius
A = (X.^2 + Y.^2 <= R^2);   % Circular aperture of radius R
M(A) = 1;                   % set mask elements inside aperture to 1
imagesc(M)                  % plot mask
axis image
n=2^10;%面具尺寸
M=零(n);
I=1:n;
x=I-n/2;%掩模x坐标
y=n/2-I;%遮罩y坐标
[X,Y]=网格(X,Y);%创建二维遮罩栅格
R=200;%孔径半径

A=(X.^2+Y.^2如果只想绘制一组圆,可以在循环中使用该函数

如果在调用
rectangle
时将
Curvature
属性设置为
1
它将被绘制为圆形(参考文档)

在循环中,您必须通过定义每个矩形(圆)的
左下
坐标及其
宽度和
高度来正确设置每个矩形(圆)的位置

使用
n
定义了圆的数量,并使用
d
定义了圆的直径,您可以计算
左下
坐标集,如下所示:

px=linspace(0,d*(nc-1),nc)
py=linspace(0,d*(nr-1),nr)
黑色背景可以通过设置轴的颜色或绘制另一个矩形来获得

然后可以将
xlim
ylim
设置为适合外部矩形

您还可以通过设置axex的○可见
属性到
off`

编辑#1

更新代码以允许绘制用户定义的圆数(设置行数和列数)

编辑#2

针对OP评论的本地解决方案:

如果我想使轴固定,比如说我想要一个1024×1024的网格(图像大小与圆半径无关),我如何将其合并到代码中

如果您想使用固定的
(1024 x 1024)
遮罩绘制圆圈,从您在问题中发布的cde开始,您只需执行以下操作:

  • 定义要在
    (1024 x 1024)
    遮罩上绘制的圈数,它们可以是
    2、4、8、
  • 定义仅包含一个圆的基本遮罩
  • 识别该圆内的点并将其设置为
    1
  • 根据要绘制的圆数,复制(使用函数)基本遮罩
根据您的代码,实现可能是:

% Define the number of circles to be plotted
% on a (1024 x 1024) mask they can be 2, 4, 8, ...
n_circles=4
% Define the size of the basic mask
n0 = (2^10)/n_circles;
% Initialize the basic mask
M0 = zeros(n0);
% Define the x and y coordinates of the basic mask
I = 1:n0;
x = I - n0/2;
y = n0/2 - I;
% Create the mask
[X,Y] = meshgrid(x,y);
% Define the radius of the basic circle
R = n0/2;
% Get the indices of the points insiede the basic circle
A = (X.^2 + Y.^2 <= R^2);
% Set basic mask
M0(A) = 1;
% Open a FIgure
figure
% Replicate the basic mask accoding to the numner of circles to be plotted
M=repmat(M0,n_circles,n_circles);
% Display the mask
imagesc(M)
% Set the axes aspect ratio
daspect([1 1 1])
%定义要打印的圆数
%在(1024x1024)掩码上,它们可以是2,4,8。。。
n_圆=4
%定义基本遮罩的大小
n0=(2^10)/n_圆;
%初始化基本掩码
M0=零(n0);
%定义基本遮罩的x和y坐标
I=1:n0;
x=I-n0/2;
y=n0/2-I;
%创建遮罩
[X,Y]=网格网格(X,Y);
%定义基本圆的半径
R=n0/2;
%获取基本圆内点的索引

A=(X.^2+Y.^2我忘了提到我必须将其保存为bmp文件。因此,当我使用您的代码并将输出图形保存为bmp文件时,结果在实际图形上有一个白色边框。无论如何,您的代码满足了我的要求。谢谢!@VironGilA.Estrada。您应该能够将轴保存为这样的图像。
fr=getframe(gca);imwrite(fr.cdata,'circle.bmp')
成功了!非常感谢!现在我可以在我的研究中取得进展了。最后一个问题。如果我调整圆圈的数量,比如从4乘4到8乘8,我应该在代码中更改什么?非常感谢!我已经更新了代码,允许绘制“用户定义”的图形圈数。只需将变量nr设置为定义行数,nc设置为定义列数。您可以使用@AeroEngy提出的解决方案将图形另存为.bmp
% Define the number of circles to be plotted
% on a (1024 x 1024) mask they can be 2, 4, 8, ...
n_circles=4
% Define the size of the basic mask
n0 = (2^10)/n_circles;
% Initialize the basic mask
M0 = zeros(n0);
% Define the x and y coordinates of the basic mask
I = 1:n0;
x = I - n0/2;
y = n0/2 - I;
% Create the mask
[X,Y] = meshgrid(x,y);
% Define the radius of the basic circle
R = n0/2;
% Get the indices of the points insiede the basic circle
A = (X.^2 + Y.^2 <= R^2);
% Set basic mask
M0(A) = 1;
% Open a FIgure
figure
% Replicate the basic mask accoding to the numner of circles to be plotted
M=repmat(M0,n_circles,n_circles);
% Display the mask
imagesc(M)
% Set the axes aspect ratio
daspect([1 1 1])