How to create a ring using meshgrid with user defined radius ?

 采纳的回答

clc; clear all ;
M = 10 ;
N = 100 ;
R1 = 0.5 ; % inner radius
R2 = 1 ; % outer radius
nR = linspace(R1,R2,M) ;
nT = linspace(0,2*pi,N) ;
%nT = pi/180*(0:NT:theta) ;
[R, T] = meshgrid(nR,nT) ;
% Convert grid to cartesian coordintes
X = R.*cos(T);
Y = R.*sin(T);
[m,n]=size(X);
% Plot grid
figure
set(gcf,'color','w') ;
axis equal
axis off
box on
hold on
% Plot internal grid lines
for i=1:m
plot(X(i,:),Y(i,:),'k','linewidth',1.5);
end
for j=1:n
plot(X(:,j),Y(:,j),'k','linewidth',1.5);
end
You may check the link:

11 个评论

Dr. Siva Srinivas Kolukula, Thanks for your answer. Is it possible to get one output variable, which contains the ring ? Because here you are plotting X and Y separately.
How you can get one output for a 2D problem? As the ring is 2D, you will have two coordinates which specify the location.
I mean, I need one 2D matrix as an image in the output
You will get 2D matrices for x and y.
Yes one 2D matrix for x and one 2D matrix for y.
K0 =[0 1] [x,y,z] = meshgrid(-A/2:(A/2)-1,-D/2:(D/2)-1,-B/2:(B/2)-1); out = exp( 1i * (k0(1) * x + k0(2) * y + k0(3)*z));
In this case the output is only one 3D matrix that is "out". I need just one output matrix like this.
"Yes one 2D matrix for x and one 2D matrix for y" it means there are 2 images that is x and y, but I only need one image as a ring. can we combine x and y to make only one matrix ?
clc; clear all ;
N = 100 ;
R1 = 0.5 ; % inner radius
R2 = 1 ; % outer radius
th = linspace(0,2*pi,N) ;
% inner circle
x1 = R1*cos(th) ; y1 = R1*sin(th) ;
% Outer circle
x2 = R2*cos(th) ; y2 = R2*sin(th) ;
% merge both
x = [x1 NaN fliplr(x2)] ;
y = [y1 NaN fliplr(y2)] ;
%%Make a mesh
x0 = min(x) ; x1 = max(x) ;
y0 = min(y) ; y1 = max(y) ;
xx = linspace(x0,x1,N) ;
yy = linspace(y0,y1,N) ;
[X,Y] = meshgrid(xx,yy) ;
%%Get points inside the ring
D = (X.^2+Y.^2) ;
D(D<R1) = NaN ;
D(D>R2) = NaN ;
pcolor(D) ;
shading interp ;
D will be a single matrix. Is it okay?
I am curious to know...why you are specific about single output?
I have 3D data and I want to correlate the 2D ring matrix with each slice of 3D data.

请先登录,再进行评论。

类别

Community Treasure Hunt

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

Start Hunting!

Translated by