如何在MATLAB中绘制圆形并在其中生成随机点

6

在此输入图片描述你好,我想问一个问题:如何在MATLAB中绘制圆并标出其圆心,并生成一定数量的随机点,例如50个?我知道用以下代码可以画出圆:

x = linspace(-sqrt(10),sqrt(10));
y1 = sqrt(10-x.^2);
y2 = -sqrt(10-x.^2);
plot(x,y1,x,y2)
axis equal
hold on

但我不知道如何在其中生成50个随机点, 然后我想到了这个伪代码,但我不知道如何用matlab编写它。

01: FOR all nodes j
 02: FOR all nodes i except node j 
03: IF distance(j to center) < distance(j to  i) AND
 04: distance(i to cell center) < distance(j to  i) 
05: THEN there's a red link from node i to node j 
06: ELSEIF distance(j to cell center) < distance(j to  i)
 07: THEN there's a blue link from node i to node j 
08: ELSE there's no D2D link from node i and j;
 09: node i has a green link with the base station 
10: END if 
11: END inner for-loop

我不明白你的问题。伪代码定义了一个链接集合,它是在一组定义好的节点上进行的,而不是生成一个随机的节点集合。 - Daniel
你想让点在圆内均匀分布吗? - pjs
顺便提一下,您可以在包含圆的正方形中生成随机点,并仅选择落在圆内的点... - bla
我希望它像我发布的图片那样,我现在已经上传了它。 - user3482135
3个回答

5

我认为这是你需要的 -

%%// Plot the circle
x = linspace(-sqrt(10),sqrt(10));
y1 = sqrt(10-x.^2);
y2 = -sqrt(10-x.^2);
plot(x,y1,x,y2)
axis equal

%%// Choose from 1000 random point pairs
N = 1000; 
%%// Radius of circle
radius = sqrt(10); 

%%// Create a random point matrix Nx2
points_mat = [ radius*2*(rand(N,1)-0.5) radius*2*(rand(N,1)-0.5)];

%%// Select the first 50 pairs that lies inside circle
ind1 = find(sqrt( points_mat(:,1).^2 + points_mat(:,2).^2 )<radius);
points_mat=points_mat(ind1(1:50),:);

%%// Plot the 50 points on the circle
hold on
text(0,0,'x Center') %%// Center
text(points_mat(:,1),points_mat(:,2),'o') %%// 50 points

绘图

图片描述


非常感谢,那么您知道在此之后如何测量圆中每个点与其余点以及每个点与中心的距离吗?就像这个伪代码一样: 01:对于所有节点j 02:对于除节点j之外的所有节点i 03:如果距离(j到中心)<距离(j到i)且 04:距离(i到单元格中心)<距离(j到i) 05:那么从节点i到节点j有一个红色链接 06:否则,如果距离(j到单元格中心)<距离(j到i) 07:那么从节点i到节点j有一个蓝色链接 08:否则,节点i与中心具有绿色链接 09:结束if 10:结束内部循环 - user3482135
2
看一下 pdist2。它正是你需要的。更多信息在这里 - http://www.mathworks.in/help/stats/pdist2.html - Divakar

4

我不了解Matlab,所以无法在这方面给您提供帮助。但是,如果您想要避免拒绝并生成极坐标下的点,则可以使用以下方法:如果rand()返回一个介于0和1之间的均匀分布的随机数,则:

r = radius * sqrt(rand())
theta = 2 * Pi * rand()
x = r * cos(theta)
y = r * sin(theta)

将产生在半径为radius的圆内均匀分布的值。请注意,在计算r时的平方根调整了距离圆心的距离的分布,以使给定距离处的点数始终与该区域的面积成比例,因此是均匀的。对于球形均匀性,您需要取立方根以保持与体积成比例,而一般情况下,k维超球体需要取kth根。


这些点在圆内不会均匀分布。它们的径向和角度位置将均匀分布,但由于面积元素为r*(dtheta)(dr),因此在大半径处有更多的面积,所以您需要在大半径处选择更多的点,以获得空间中的均匀分布。更多信息请参见:https://dev59.com/wG025IYBdhLWcg3wtobX - Doug Lipinski
我在那里错过了 sqrt,非常抱歉。是我的错误。不幸的是,除非发布者进行编辑,否则我的踩已经被锁定了 :( 你可以考虑进行编辑以添加额外的解释,我很乐意切换为点赞。 - Doug Lipinski
@DougLipinski 解释很好,我之前应该加上一个解释,所以感谢您的建议。 - pjs

0

这里还有另外一个选项:

%// Set parameters
R = 0.5;   %// radius
C = [3 4]; %// center [x y]
N = 50;    %// number of points inside circle

%// generate circle boundary
t = linspace(0, 2*pi, 100);
x = R*cos(t) + C(1);
y = R*sin(t) + C(2);

%// generate random points inside it
th = 2*pi*rand(N,1);
r  = R*rand(N,1);

xR = r.*cos(th) + C(1);
yR = r.*sin(th) + C(2);

%// Plot everything
figure(1), clf, hold on
plot(x,y,'b')
plot(C(1),C(2),'r.', 'MarkerSize', 100)
plot(xR,yR,'k.')
axis equal

enter image description here

以下是这个功能有用的原因:

%// Set parameters
R = 0.5;     N = 50;
C = [3 4];   M = 100;  %// points on boundary

%// generate all points at once
t  = linspace(0, 2*pi, M)';
th = 2*pi*rand(N,1);
r  = R*rand(N,1);
xR = [R*ones(M,1); r] .* cos([t; th]) + C(1);
yR = [R*ones(M,1); r] .* sin([t; th]) + C(2);

%// Plot everything
figure(1), clf, hold on
plot(xR(1:M),yR(1:M),'b')                %// circle boundary
plot(C(1),C(2),'r.', 'MarkerSize', 100)  %// center
plot(xR(M+1:end),yR(M+1:end),'k.')       %// random points
axis equal

非常感谢,您知道在此之后如何测量圆中每个点与其余点以及每个点与圆心的距离吗?请看上面的图片和伪代码: 01: 对于所有点j 02: 对于除了点j以外的所有点i 03: 如果距离(j到圆心)<距离(j到i)并且 04:距离(i到单元格中心)<距离(j到i)05: 那么从节点i到节点j有一个红色链接 06: 否则,如果距离(j到单元格中心)<距离(j到i),则从节点i到节点j有一个蓝色的链接07: 否则节点i与中心之间有一个绿色链接09: 结束if 10: 结束内部循环 - user3482135

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接