我想生成一组在半径为R的球内随机分布的坐标,有没有办法在Matlab中以类似矩阵的形式,在没有for循环的情况下做到这一点?
谢谢
更新:很抱歉给你带来了混乱。我只需要在半径为R的圆上随机生成n个点,而不是球体。
发布于 2011-04-12 13:01:26
正确答案在这里,http://mathworld.wolfram.com/DiskPointPicking.html。这种分布称为“圆盘点拾取”。
发布于 2011-04-12 09:11:18
我正要把这个标记为之前在generating uniform distribution of points in a sphere上的一个问题的副本,但我认为你值得怀疑,因为尽管问题中有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);要了解为什么不能像人们可能认为的那样对所有三个坐标都使用统一的随机变量是正确的方法,give this article a read。
发布于 2011-04-12 11:04:19
为了完整起见,这里有一些用于点剔除解决方案的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因子(即单位立方体体积与单位球体的体积之比)放大,则可以接近球体中所需的点数。然而,由于我们毕竟是在处理(伪)随机数,我们永远不能绝对确定我们是否会生成足够的落入球体中的点。
简而言之,如果您想生成特定数量的点,我会尝试建议的其他解决方案之一。否则,点剔除解决方案是很好和简单的。
https://stackoverflow.com/questions/5628973
复制相似问题