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 the sample 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.A 4 x 4 array of circles.
If you just want to plot a set of circles, you can use the rectangle function within a loop
If in the call to
rectangle
you set theCurvature
property to1
it will be drawn as circle (ref. to the documentation).In the loop you hav to properly set the position of each rectangle (circle) by defining its
lower left
coordinates along with itswidth
andheight
.Having defined with
n
the number of circles and withd
their diameter, you can compute the set oflower left
coordinates as:The black background can be either obtained by setting the color of the axes or by plotting another rectangle.
Then you can set the
xlim
andylim
to fit with the external rectangle.You can also make the axex invisible, by setting its ○Visible
property to
off`.Edit #1
Code updated to allow drawing a user-defined number of circles (set the number of rows and the number of columns)
Edit #2
Aternative solution to address the OP comment:
If I want to make the axes fixed, say I want a meshgrid of 1024 by 1024, (the image size is independent of the circle radius) how do I incorporate it in the code?
If you want to use a fixed
(1024 x 1024)
mask on which to plot the circles, starting from the cde you've posted in the question, you can simply do the following:(1024 x 1024)
mask they can be2, 4, 8, ...
1
The implemntation, based on your code could be: