How to plot an NxN array of circles?

조회 수: 6 (최근 30일)
Viron Gil Estrada
Viron Gil Estrada 2018년 2월 11일
댓글: Jos (10584) 2018년 2월 12일

I want to plot an NxN array of circles. Just to visualize, I attached an image of what I want to achieve. I'm new in MatlLab so I tried to plot a single circle first, and here is my code below:

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

I really don't have any idea on how to plot a 2D-array of circles. The distance between two circles is two radii. I need this for my research. Hoping anyone can help.

채택된 답변

Geoff Hayes
Geoff Hayes 2018년 2월 11일
Viron - you could just create one circle within a 2-D array whose number of rows and columns are identical to the diameter of your circle. For example, using code similar to yours
r = 50;
n = 1000;
X = repmat(linspace(-r,r,n), n, 1);
Y = rot90(X);
C = X.^2 + Y.^2 <= r^2;
imagesc(C)
axis image
The radius r for our circle is 50. We use linspace to create a row array of n elements from -r to r (so the interval [-50,50]). We then use repmat to repeat this row n times. We then rotate this matrix by 90 degrees counter-clockwise to get the Y (this should be identical to your use of meshgrid). We then create one circle as you have already done with the only difference being that the circle of radius 50 is now inside a square with the a length and width being the diameter of your circle.
We can then again use repmat to repeat the C to how many circles we want in the x and y direction.
nc = 10;
MC = repmat(C, nc, nc); % multiple circles
imagesc(MC)
axis image
  댓글 수: 3
Viron Gil Estrada
Viron Gil Estrada 2018년 2월 12일
How do I modify the code if I want the meshgrid to be fixed, say 1024 by 1024 grid? Thanks in advance!
Jos (10584)
Jos (10584) 2018년 2월 12일
n determines the number of points in the grid

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by