Plotting 2D arrays of concentric circles in a meshgrid.

7 vues (au cours des 30 derniers jours)
Viron Gil Estrada
Viron Gil Estrada le 15 Mar 2018
I now have two separate codes for plotting N by N arrays of solid circles and for plotting concentric circles.
%Plotting an N by N arrays of circles
clc; clear;
n_circles = 4; % Define the number of circles to be plotted
R = 63; % Define the radius of the basic circle
Len=1024;
M=zeros(Len); % Create the hole mask
% Get the indices of the points inside the basic circle
M0 = zeros(2*R+1); % Initialize the basic mask
I = 1:(2*R+1); % Define the x and y coordinates of the basic mask
x = (I - R)-1;
y = (R - I)+1;
[X,Y] = meshgrid(x,y); % Create the mask
A = (X.^2 + Y.^2 <= R^2);
[xx,yy]=ind2sub(size(M0),find(A == true));
%plot
for ii=1:n_circles
for jj=1:n_circles
MidX=Len/2+(ii-n_circles/2-0.5)*(2*R);
MidY=Len/2+(jj-n_circles/2-0.5)*(2*R);
% [MidX MidY]
M(sub2ind(size(M),MidX+xx-R-1,MidY+yy-R-1))=1;
end
end
figure(1)
imshow(M)
The second code below is for generating concentric circles using meshgrid.
N=1024; % some size of grid
if mod(N,2) % odd vs even matrix sizes
[x,y] = meshgrid(-(N-1)/2:(N-1)/2);
else
[x,y] = meshgrid(-N/2+1:N/2);
end
x0=0; y0=0; % assuming the circle is always at the origin, but wew can modify this if needed
% Rings between `r1` and `r2`
f = @(r1,r2) (x-x0).^2+(y-y0).^2<=r2^2 & ...
(x-x0).^2+(y-y0).^2>=r1^2;
R = f(70,90)+f(100,120)+f(130,150)
imagesc(R)
axis equal
How do I combine these two set of codes in such a way that the final result will generate an N by N (2D) arrays of concentric circles?
Thanks,

Réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by