randsph.m

来自「模式识别工具箱,希望对大家有用!」· M 代码 · 共 30 行

M
30
字号
function x = randsph(n,d)
%RANDSPH generate objects in hypersphere
%
% x = randsph(n,d)
%
% Generate n data objects uniformly drawn from a d-dimensional hypersphere
% with zero mean and unit radius.

% Copyright: D. Tax, R.P.W. Duin, davidt@ph.tn.tudelft.nl
% Faculty of Applied Physics, Delft University of Technology
% P.O. Box 5046, 2600 GA Delft, The Netherlands

% Gaussian data:
x = randn(n,d);
% squared norms of the vectors:
nx = sum(x.*x,2);
% make squared norms uniformly distributed:
if exist('mychi2cdf')  % necessary when you have your own copy of
                       % chi2cdf
  rx = mychi2cdf(nx,d);
else
  rx = chi2cdf(nx,d);
end
% change uniform to r^d
newrx = rx.^(2/d);

% renormalize the original data x:
x = sqrt(newrx./nx)*ones(1,d).*x;

⌨️ 快捷键说明

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