圆的几何随机图

我想生成一组在半径为R的球内随机均匀分布的坐标。有没有任何方法可以在没有for循环的Matlab中以矩阵形式执行此操作? 谢谢 更新: 对不起,我很抱歉。我只需要在半径为R的圆上随机均匀地生成n个点,而不是球体。     
已邀请:
正确的答案在这里http://mathworld.wolfram.com/DiskPointPicking.html。分发称为“磁盘点选择”     
我打算将此标记为关于在球体中生成点的均匀分布的先前问题的副本,但我认为你在这里应该受到怀疑,因为虽然问题中有一个matlab脚本,但大部分线程都是python 。 问题中给出的这个小函数(我直接从那里粘贴)就是你所需要的。
function X = randsphere(m,n,r)

% This function returns an m by n array, X, in which 
% each of the m rows has the n Cartesian coordinates 
% of a random point uniformly-distributed over the 
% interior of an n-dimensional hypersphere with 
% radius r and center at the origin.  The function 
% 'randn' is initially used to generate m sets of n 
% random variables with independent multivariate 
% normal distribution, with mean 0 and variance 1.
% Then the incomplete gamma function, 'gammainc', 
% is used to map these points radially to fit in the 
% hypersphere of finite radius r with a uniform % spatial distribution.
% Roger Stafford - 12/23/05

X = randn(m,n);
s2 = sum(X.^2,2);
X = X.*repmat(r*(gammainc(s2/2,n/2).^(1/n))./sqrt(s2),1,n);
要了解为什么你不能只对所有三个坐标使用均匀随机变量,因为人们可能认为这是正确的方法,请阅读本文。     
为了完整起见,这里有一些用于点剔除解决方案的MATLAB代码。它在单位立方体内生成一组随机点,移除单位球体外的点,并将坐标点向上缩放以填充半径为
R
的球体:
XYZ = rand(1000,3)-0.5;           %# 1000 random 3-D coordinates
index = (sum(XYZ.^2,2) <= 0.25);  %# Find the points inside the unit sphere
XYZ = 2*R.*XYZ(index,:);          %# Remove points and scale the coordinates
这种点剔除方法的一个主要缺点是难以产生特定数量的点。例如,如果要在球体内生成1000个点,那么在剔除它们之前,您需要在多维数据集中创建多少个点?如果将立方体中生成的点数按比例缩放
6/pi
(即单位立方体的体积与单位球体的比率),则可以接近球体中所需点的数量。然而,由于我们毕竟处理(伪)随机数,我们永远不能绝对肯定我们会产生足够的点落在球体中。 简而言之,如果您想生成特定数量的点,我会尝试建议的其他解决方案之一。否则,点剔除解决方案很简单。     
不确定我是否正确理解你的问题,但是你不能通过设置φ,θ和r来分配给随机数来在球体内生成任何随机数吗?     

要回复问题请先登录注册