How to plot an NxN array of circles?

8 views (last 30 days)

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.

Accepted Answer

Geoff Hayes
Geoff Hayes on 11 Feb 2018
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 Comments
Viron Gil Estrada
Viron Gil Estrada on 12 Feb 2018
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) on 12 Feb 2018
n determines the number of points in the grid

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!