用Matlab将椭圆绘制成矩阵图像
我想把一个椭圆画成一个矩阵图像来建立一个shepp-logan模型,它是一个椭圆边的集合,我只想画一个椭圆,然后把它转换成矩阵图像,如果你有PDE工具箱可以使用的话。否则,您可以只写:用Matlab将椭圆绘制成矩阵图像,matlab,Matlab,我想把一个椭圆画成一个矩阵图像来建立一个shepp-logan模型,它是一个椭圆边的集合,我只想画一个椭圆,然后把它转换成矩阵图像,如果你有PDE工具箱可以使用的话。否则,您可以只写: % input ellipse parameters theta_grid = linspace(0,2*pi); phi = 45*180/pi; X0=10; Y0=20; a=40; b=15; % the ellipse in x and y coordinates ellipse_x_r = X0
% input ellipse parameters
theta_grid = linspace(0,2*pi);
phi = 45*180/pi;
X0=10;
Y0=20;
a=40;
b=15;
% the ellipse in x and y coordinates
ellipse_x_r = X0 + a*cos( theta_grid );
ellipse_y_r = Y0 + b*sin( theta_grid );
%Define a rotation matrix
R = [ cos(phi) sin(phi); -sin(phi) cos(phi) ];
%let's rotate the ellipse to some angle phii
r_ellipse = R * [ellipse_x_r;ellipse_y_r];
plot(r_ellipse(1,:),r_ellipse(2,:),'-x')
这里有另一个选项,它可以代替x-y坐标,将椭圆“戳”到数组中:
a=20;
b=9;
phi=45;
[x y] = meshgrid(-50:50,-50:50);
el=((x-X0)/a).^2+((y-Y0)/b).^2<=1;
imagesc(imrotate(el,phi)); colormap(bone)
a=20;
b=9;
φ=45;
[xy]=网格(-50:50,-50:50);
el=((x-X0)/a)。^2+((y-Y0)/b)。^2如果您有PDE工具箱,您可以使用。否则,您可以只写:
% input ellipse parameters
theta_grid = linspace(0,2*pi);
phi = 45*180/pi;
X0=10;
Y0=20;
a=40;
b=15;
% the ellipse in x and y coordinates
ellipse_x_r = X0 + a*cos( theta_grid );
ellipse_y_r = Y0 + b*sin( theta_grid );
%Define a rotation matrix
R = [ cos(phi) sin(phi); -sin(phi) cos(phi) ];
%let's rotate the ellipse to some angle phii
r_ellipse = R * [ellipse_x_r;ellipse_y_r];
plot(r_ellipse(1,:),r_ellipse(2,:),'-x')
这里有另一个选项,它可以代替x-y坐标,将椭圆“戳”到数组中:
a=20;
b=9;
phi=45;
[x y] = meshgrid(-50:50,-50:50);
el=((x-X0)/a).^2+((y-Y0)/b).^2<=1;
imagesc(imrotate(el,phi)); colormap(bone)
a=20;
b=9;
φ=45;
[xy]=网格(-50:50,-50:50);
el=((x-X0)/a)。^2+((y-Y0)/b)。^2嘿,我认为给出的解决方案有问题。
在平移完成后应用旋转,这会导致一些wierd结果(如果您查看它,绘制的椭圆的中心不是(10,20)),因为旋转是以中心为0定义的
我相信正确的答案是:
theta_grid = linspace(0,2*pi);
phi = 45*180/pi;
X0=10;
Y0=20;
a=40;
b=15;
% the ellipse in x and y coordinates centered at 0
ellipse_x_r = a*cos( theta_grid );
ellipse_y_r = b*sin( theta_grid );
% Define a rotation matrix
R = [ cos(phi) sin(phi); -sin(phi) cos(phi) ];
n = length(ellipse_x_r);
% let's rotate the ellipse to some angle phii and then translate it to (X0, Y0)
r_ellipse = R * [ellipse_x_r; ellipse_y_r] + repmat([X0; Y0], [1, n]);
plot(r_ellipse(1,:),r_ellipse(2,:),'-x')
感谢您提供的解决方案,尽管我在任何地方都找不到如何做到这一点,这是我找到的最有用的帖子。
干杯
巴勃罗
编辑:嘿,SO的代码格式化程序有一个bug。注释中的“A”不是字符串=)嘿,我认为给出的解决方案存在问题。
在平移完成后应用旋转,这会导致一些wierd结果(如果您查看它,绘制的椭圆的中心不是(10,20)),因为旋转是以中心为0定义的
我相信正确的答案是:
theta_grid = linspace(0,2*pi);
phi = 45*180/pi;
X0=10;
Y0=20;
a=40;
b=15;
% the ellipse in x and y coordinates centered at 0
ellipse_x_r = a*cos( theta_grid );
ellipse_y_r = b*sin( theta_grid );
% Define a rotation matrix
R = [ cos(phi) sin(phi); -sin(phi) cos(phi) ];
n = length(ellipse_x_r);
% let's rotate the ellipse to some angle phii and then translate it to (X0, Y0)
r_ellipse = R * [ellipse_x_r; ellipse_y_r] + repmat([X0; Y0], [1, n]);
plot(r_ellipse(1,:),r_ellipse(2,:),'-x')
感谢您提供的解决方案,尽管我在任何地方都找不到如何做到这一点,这是我找到的最有用的帖子。
干杯
巴勃罗
编辑:嘿,SO的代码格式化程序有一个bug。注释中的“”不是字符串=)