如何在matlab中画圆并在其中生成随机点
你好,我想问一个问题,如何在matlab中画一个圆,标记它的中心,并在其中生成一定数量的随机点,例如50?我知道这个代码是用来画圆圈的如何在matlab中画圆并在其中生成随机点,matlab,random,geometry,point,Matlab,Random,Geometry,Point,你好,我想问一个问题,如何在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
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
01:对于所有节点j
02:对于除节点j以外的所有节点i
03:如果距离(j到中心)<距离(j到i)和
04:距离(i到单元中心)<距离(j到i)
05:然后从节点i到节点j有一个红色链接
06:ELSEIF距离(j到单元中心)<距离(j到i)
07:然后是从节点i到节点j的蓝色链接
08:否则节点i和j没有D2D链接;
09:节点i与基站之间有一条绿色链路
10:如果结束
11:内环的端部
你认为这就是你需要的吗-
%%// 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
%%//绘制圆
x=linspace(-sqrt(10),sqrt(10));
y1=sqrt(10-x.^2);
y2=-sqrt(10-x.^2);
绘图(x,y1,x,y2)
轴相等
%%//从1000个随机点对中选择
N=1000;
%%//圆半径
半径=sqrt(10);
%%//创建一个随机点矩阵Nx2
点_mat=[半径*2*(兰特(N,1)-0.5)半径*2*(兰特(N,1)-0.5)];
%%//选择位于圆内的前50对
ind1=find(sqrt(points_-mat(:,1)。^2+points_-mat(:,2)。^2)我不懂matlab,所以我无法在这方面帮助您,但如果您希望不被拒绝地完成此操作,您可以在极坐标中生成点。如果rand()
返回统一(0,1)随机数,则:
r = radius * sqrt(rand())
theta = 2 * Pi * rand()
x = r * cos(theta)
y = r * sin(theta)
将产生在半径为的圆内均匀分布的值半径
。请注意计算r
时的平方根,该平方根调整距圆心的距离分布,以便给定距离处的点数始终与面积成比例,因此是均匀的。对于spherical均匀性您可以使用立方根来保持与体积的比例,对于k维超球体,通常使用第k个立方根。这里是另一个选项:
%// 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
以下是这可能有用的原因:
%// 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
我不明白你的问题。peudo代码在定义的节点集上定义了一个链接集,它不会生成一个随机的节点集。你想让点均匀分布在圆中吗?顺便说一下,你可以在包含圆的正方形中生成随机点,并且只选择落在圆中的那些点…我想这样做e图片在我的帖子中我现在上传了它非常感谢你,所以你知道在这之后我如何测量圆中的每个点和其余点之间的距离,以及每个点和中心之间的距离,如伪代码01:对于所有节点j 02:对于除节点j以外的所有节点i 03:如果距离(j到中心)<距离(j到i)和04:距离(i到小区中心)pdist2
。它正是这样做的。这里的更多信息-这些点在圆中不会均匀分布。它们的径向和角度位置将均匀分布,但由于e area元素是r*(dtheta)(dr)在大半径处有更多的区域,因此您需要在大半径处选择更多点,以在空间中均匀分布。更多信息:我错过了那里的sqrt
,对此非常抱歉。我的错误。不幸的是,除非编辑帖子,否则我的下一票现在看起来已锁定:(你可能会考虑编辑来增加你的解释,我会高兴地转换为一个投票。”道格利宾斯解释很好,我应该先把它放进去,谢谢你的建议。中心如上图所示,伪代码a 01:对于所有点j 02:对于除点j 03以外的所有点i:如果距离(j到中心)<距离(j到i)和04:距离(i到单元中心)<距离(j到i)05:则从节点i到节点j 06有一个红色链接:ELSEIF距离(j到单元中心)<距离(j到i)07:然后从节点i到节点j有一个蓝色链接08:否则节点i有一个绿色链接,中心是09:END if 10:END in for循环