如何在Matlab中制作圆并在其中生成随机点 [英] how to make circle in matlab and generate random points inside it

查看:493
本文介绍了如何在Matlab中制作圆并在其中生成随机点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我想问一个问题,如何在matlab中画一个圆并标记它居中并在其中生成一定数量的随机点,例如50?我知道这段代码会绕圈

hello i want to ask a question how to make a circle in matlab and mark its center and generate a certain number of random points inside it for example 50 ? i know this code to make a circle

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中编写它

but i don't know how to generate 50 random points inside it then i thought of this pseudo code but i don't know how to write it in 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

情节

这篇关于如何在Matlab中制作圆并在其中生成随机点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆