matlab - 如何在matlab中制作圆圈并在其中生成随机点

标签 matlab random geometry point

enter image description here你好我想问一个问题如何在 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

最佳答案

认为这就是您需要的 -

%%// 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

绘图

enter image description here

关于matlab - 如何在matlab中制作圆圈并在其中生成随机点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22768349/

相关文章:

php mysql返回可变数量

java - 如何安排随机时间(每隔随机分钟)事件

c - OpenGL - 在没有旋转的情况下绕着太阳旋转月亮?

numpy - 在 3D numpy 网格中绘制/采样球体

matlab - 如何用拉普拉斯算子+对角矩阵高效求解线性方程组?

c++ - C++ 中的 MATLAB 等效项

matlab - 如何获取二值图像轮廓的坐标?

c# - 使用 RNGCryptoServiceProvider 生成随机字符串

matlab - 如何在直方图箱上方显示标签?

algorithm - 如何最快地检查点(3D)是否在点集给出的凸包内