rand_uniform_inside_hypersphere.m~

来自「一个用EM算法的源程序」· M~ 代码 · 共 35 行

M~
35
字号
function X = rand_uniform_inside_hypersphere(ambientDimension, sampleCount)% Generate sampleCount points sampled uniformly from within a unit hypersphere% lying in an ambient space of dimension ambientDimension.% Columns of X are data points.% if ambientDimension == 1,%     % For One Dimension, the sphere is just the two points {-1, 1}, thus we%     % just sample uniformly in {-1, 1}%     X = 2*rand(1,sampleCount)-1;%     % elseif ambientDimension == 2,%     % For Two Dimensions, the sphere is just a circle in R2.%     k = 1000;%     theta = 2*pi*rand(1,sampleCount);%     radii = rand(1, sampleCount).^.5;%     X = [radii.*sin(theta); radii.*cos(theta)];% % elseif ambientDimension > 2,% Let the spherical symmetry of a vector of gaussians do most of the% hard work for us.  Once we have a bunch of points projected onto a% sphere, rescaling them by u^(1/ambientDimension) where u \in [0,1]% will create a uniform density of points in the sphere.% There are ways of doing this that don't require the computation of so% many random numbers.  There is an article from the seventies on how% to do this written in an era where this sort of thing took hours on% contemporary hardware.X = rand(ambientDimension, sampleCount);norms = sqrt(sum(X.*X,1));radii = rand(1, sampleCount).^(1/ambientDimension);X = X * diag(sparse(radii./norms));% end

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?