我想生成一组在半径为 R 的球内随机均匀分布的坐标。在 Matlab 中有没有任何方法可以在没有 for 循环的情况下以类似矩阵的形式做到这一点?
谢谢
更新:我很抱歉造成混乱。我只需要在半径为 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 个点,那么在剔除它们之前您必须在立方体中创建多少个点?如果将立方体中生成的点数放大 1 倍6/pi
(即单位立方体的体积与单位球体的体积之比),则可以接近球体中所需的点数。然而,由于我们毕竟处理的是(伪)随机数,我们永远不能绝对确定我们会生成足够多的点落在球体中。
简而言之,如果您想生成特定数量的点,我会尝试建议的其他解决方案之一。否则,点剔除解决方案既好又简单。
不确定我是否正确理解了您的问题,但是您不能通过设置分配给随机数的 φ、θ 和 r 在球体内生成任何随机数吗?