你好我想问一个问题,如何在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
答案 0 :(得分: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
<强>剧情强>
答案 1 :(得分:4)
我不知道matlab,所以我无法帮助你,但是如果你想在没有拒绝的情况下这样做,你可以在极坐标中生成点。如果rand()
返回统一(0,1)随机数,则:
r = radius * sqrt(rand())
theta = 2 * Pi * rand()
x = r * cos(theta)
y = r * sin(theta)
将产生均匀分布在半径radius
的圆内的值。注意计算r
时的平方根,它调整距离圆心的距离分布,使得给定距离处的点数总是与面积成比例,因此是均匀的。对于球形均匀性,你需要立方根来保持与体积的比例,一般来说k th 根对于k维超球面。
答案 2 :(得分: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
这就是为什么这可能有用:
%// 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