如何在椭圆matlab中绘制随机点

azd*_*oud 3 matlab

我想用这个椭圆填充其中的N个随机点,任何帮助我都会很高兴

clear ,close all;
xCenter = 15;
yCenter = 10;
xRadius = 3.5;
yRadius = 8;
theta = 0 : 0.01 : 2*pi;
N = 100; % N rand points

x = xRadius * cos(theta) + xCenter;
y = yRadius * sin(theta) + yCenter;    

plot(x, y, 'LineWidth', 1);

axis square;

grid on;
Run Code Online (Sandbox Code Playgroud)

我尝试使用此代码在椭圆内部生成具有特定参数的100个点,但我没有达到目标,

xCenter = 5;
yCenter = 3;
xRadius = 3.5;
yRadius = 8;
theta = 0 : 0.01 : 2*pi;
N = 100;

x = xRadius * cos(theta) + xCenter;
y = yRadius * sin(theta) + yCenter;

xq=(rand(N,1)*(2*yRadius) - yRadius);
yq=(rand(N,1)*(2*yRadius) - yRadius);    

in = inpolygon(xq,yq,x,y);
hold on
inX = xq(in);
inY = yq(in);
outX = xq(~in);
outY = yq(~in);
plot(inX, inY , 'ro');
plot(outX, outY, 'b*');
plot(x, y, 'LineWidth', 1);

axis square;

grid on;
Run Code Online (Sandbox Code Playgroud)

Cri*_*ngo 6

Sardar的答案产生的点不均匀分布在椭圆内.此代码生成均匀的点分布:

xCenter = 15;
yCenter = 10;
xRadius = 3.5;
yRadius = 8;
N = 100;

% Generate points in the ellipse
t = 2*pi * rand(N,1);
d = sqrt(rand(N,1));
x = xCenter + xRadius * d .* cos(t);
y = yCenter + yRadius * d .* sin(t);
plot(x,y,'o')
Run Code Online (Sandbox Code Playgroud)

不同之处在于sqrt与原点的标准化(0到1)距离d.通过计算此平方根,可以增加更接近椭圆边缘的点的密度.这补偿了在中心附近过于密集的点.沿着该归一化距离的点的均匀分布是导致中心附近的点密度更高的原因.