Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Random vectors

Subject: Random vectors

From: Mauro Santos

Date: 30 Sep, 2003 14:40:53

Message: 1 of 14

Does anyone know how to generate random vectors as points on a
n-dimensional unit sphere?

Thanks,

Mauro

Subject: Random vectors

From: Kris Smolders

Date: 30 Sep, 2003 16:37:08

Message: 2 of 14

I would say: calculate n-1 random numbers between -1 and 1 then calculate
the n-coordinate of the vector using your constraint: sqrt(sum(xi^2)) = 1 =>
xn = ... Use a random number between 0 and 1 to asign a sign to xn (for
example: if rand > 0.5 => sign = + else sign = -).

Kris

"Mauro Santos" <mauro.santos@uab.es> wrote in message
news:blbtkk$kej$1@noucreus-atm.cesca.es...
> Does anyone know how to generate random vectors as points on a
> n-dimensional unit sphere?
>
> Thanks,
>
> Mauro
>

Subject: Random vectors

From: AJ \"no z\" Johnson

Date: 30 Sep, 2003 10:35:47

Message: 3 of 14

"Mauro Santos" <mauro.santos@uab.es> wrote in message
news:blbtkk$kej$1@noucreus-atm.cesca.es...
> Does anyone know how to generate random vectors as points on a
> n-dimensional unit sphere?
>
> Thanks,
>
> Mauro
>

v = randn(1,n); % A vector with random "direction"
v = v./sqrt(v*v'); % Normalize (on unit sphere)

-Aj

Subject: Random vectors

From: Andrew M. Ross

Date: 30 Sep, 2003 12:01:18

Message: 4 of 14

Mauro Santos wrote:
> Does anyone know how to generate random vectors as points on a
> n-dimensional unit sphere?

AJ "no z" Johnson wrote:
 > v = randn(1,n); % A vector with random "direction"
 > v = v./sqrt(v*v'); % Normalize (on unit sphere)

Kris Smolders wrote:
 >I would say: calculate n-1 random numbers between -1 and 1 then
 >calculate the n-coordinate of the vector using your constraint:
 >sqrt(sum(xi2)) = 1 =>
 >xn = ... Use a random number between 0 and 1 to asign a sign to xn (for
 >example: if rand > 0.5 => sign = + else sign = -).

Unfortunately, neither of these give a uniform distribution over the
sphere. The first has more mass near the corners of the unit n-cube,
while the second can fail to produce a point on the unit sphere (imagine
if x(1)=x(2)=...=x(n-1)=0.9999, then no value of x(n) will be able to
rescue it.

It's an interesting exercise, actually, to try the first suggestion in 2
dimensions, and plot a histogram of the resulting angles. You should
see four equal-size peaks.

But, back to the question (which I've also seen as a homework question,
so I'm going to be a bit vague): the solution where you start with a
point in the unit cube and then normalize is close to correct. Except,
you want to reject any points that are not inside the unit sphere.

I think there have been discussions of this problem on this newsgroup,
or possibly sci.math.num-analysis, in the not-too-distant past, as well.

Andrew

--
Andrew Ross
Industrial and Systems Engineering
Lehigh University, www.lehigh.edu/~inime/
Bethlehem, Pennsylvania, USA
remove all digits <=4 from my e-mail address to reply

Subject: Random vectors

From: Pierpa

Date: 30 Sep, 2003 12:29:06

Message: 5 of 14

> imagine if x(1)=x(2)=...=x(n-1)=0.9999,
> then no value of x(n) will be able
> to rescue it.


That event has prob 0.
By def. the isotropic Gaussian is uniform on the unit sphere [just
move to spherical coordinates]. One of the most famous way to
simulate from Gaussian is based on this property.


HTH


--P

Subject: Random vectors

From: John D'Errico

Date: 30 Sep, 2003 21:02:44

Message: 6 of 14

In article <blbtkk$kej$1@noucreus-atm.cesca.es>,
 Mauro Santos <mauro.santos@uab.es> wrote:

> Does anyone know how to generate random vectors as points on a
> n-dimensional unit sphere?

Simplest is to over-sample from an n-cube, and
then reject all those which fall outside. Its easy
enough to compute the n-d volumes of the cube and
sphere in advance giving you the required extent
of over-sampling.

Another aproach is to sample (uniform) random
sphere angles, then generate a random radius
from the approriate distribution. This seems
easy enough to do in 2d or 3d. For example:

n=100;
theta=rand(n,1)*2*pi;
r=sqrt(rand(n,1));
xy=[r.*cos(theta), r.*sin(theta)];

HTH,
John D'Errico


--
There are no questions "?" about my real address.

Subject: Random vectors

From: Mauro Santos

Date: 1 Oct, 2003 11:11:26

Message: 7 of 14



AJ "no z" Johnson wrote:
> "Mauro Santos" <mauro.santos@uab.es> wrote in message
> news:blbtkk$kej$1@noucreus-atm.cesca.es...
>
>>Does anyone know how to generate random vectors as points on a
>>n-dimensional unit sphere?
>>
>>Thanks,
>>
>>Mauro
>>
>
>
> v = randn(1,n); % A vector with random "direction"
> v = v./sqrt(v*v'); % Normalize (on unit sphere)
>
> -Aj
>
>
I was 'checking' this sugestion with the more cumbersome and dirty
method of generating pc components from independent random samples from
a multivariate normal distribution. Something like this:

mu = zeros(1,n);
sigma = eye(n);
sample1 = mvnrnd(mu,sigma,100);
sample2 = mvnrnd(mu,sigma,100);
pc1 = pcacov(sample1);
pc2 = pcacov(sample2);
theta = acos(abs(pc1(:,1)'*pc2(:,1))); %absolute angle

The distribution of absolute angles seems to be quite similar with both
methods. More tips?

Many thanks for your answers.

Mauro Santos

Subject: Random vectors

From: John Lansberry

Date: 1 Oct, 2003 07:48:26

Message: 8 of 14

You might want to check out mathworld.wolfram.com.

Look for Sphere Point Picking and Hypersphere Point Picking.

John

"Mauro Santos" <mauro.santos@uab.es> wrote in message
news:blbtkk$kej$1@noucreus-atm.cesca.es...
> Does anyone know how to generate random vectors as points on a
> n-dimensional unit sphere?
>
> Thanks,
>
> Mauro
>

Subject: Random vectors

From: Pierpa

Date: 1 Oct, 2003 09:43:49

Message: 9 of 14

You might find this book useful:
- Mark E. Johnson (1987)
  "Multivariate Statistical Simulation:
   A Guide to Selecting and Generating
   Continuous Multivariate Distributions",
   John Wiley & Sons


Anyway, in general, if you know the parametric definition of a
surface, you can play around with that as in the following case:


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Uniform on a Moebius'string
%
r = 3; R = 10;
np = 100; nr = 20;
N = 10000;


phi = linspace(0,2*pi,100);
rho = linspace(-r,r,nr);


[PHI,RHO] = meshgrid(phi,rho);


% Parametric def.
THETA = PHI/2;
X = (R+RHO.*sin(THETA)).*cos(PHI);
Y = (R+RHO.*sin(THETA)).*sin(PHI);
Z = RHO.*cos(THETA);


% Sample
xi = rand(N,1);
eta = rand(N,1);
rho = (1-2*xi)*r;
theta = pi*eta;
for k=1:10
    theta = pi*eta+rho./R.*cos(theta);
end
x = (R+rho.*sin(theta)).*cos(2*theta);
y = (R+rho.*sin(theta)).*sin(2*theta);
z = rho.*cos(theta);


plot3(x,y,z,'.')
axis('equal')
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


HTH


--P

Subject: Random vectors

From: AJ \"no z\" Johnson

Date: 1 Oct, 2003 10:56:42

Message: 10 of 14

"Andrew M. Ross" <amr54321@lehigh.edu> wrote in message
news:blc9e7$gm4@fidoii.CC.Lehigh.EDU...
> Mauro Santos wrote:
> > Does anyone know how to generate random vectors as points on a
> > n-dimensional unit sphere?
>
> AJ "no z" Johnson wrote:
> > v = randn(1,n); % A vector with random "direction"
> > v = v./sqrt(v*v'); % Normalize (on unit sphere)
>
> Unfortunately, neither of these give a uniform distribution over the
> sphere. The first has more mass near the corners of the unit n-cube,
> [...]
>
> It's an interesting exercise, actually, to try the first suggestion in 2
> dimensions, and plot a histogram of the resulting angles. You should
> see four equal-size peaks.
>
[...]
> Andrew
>

Andrew,
I do not agree with you. I believe the Guassian nature of the distribution
gives equal expectation (density) in all directions, as evidenced by:
  nn=1000000;r=randn(1,nn)+j*randn(1,nn); hist(angle(r),1000)
The same is not true for uniform distributions.
-Aj

Subject: Random vectors

From: Andrew M. Ross

Date: 1 Oct, 2003 11:45:24

Message: 11 of 14

AJ "no z" Johnson wrote:
> "Andrew M. Ross" <amr54321@lehigh.edu> wrote in message
> news:blc9e7$gm4@fidoii.CC.Lehigh.EDU...
>
>>Mauro Santos wrote:
>>
>>>Does anyone know how to generate random vectors as points on a
>>>n-dimensional unit sphere?
>>
>>AJ "no z" Johnson wrote:
>> > v = randn(1,n); % A vector with random "direction"
>> > v = v./sqrt(v*v'); % Normalize (on unit sphere)
>>
>>Unfortunately, neither of these give a uniform distribution over the
>>sphere. The first has more mass near the corners of the unit n-cube,
>>[...]
>>It's an interesting exercise, actually, to try the first suggestion in 2
>>dimensions, and plot a histogram of the resulting angles. You should
>>see four equal-size peaks.
>>Andrew
> Andrew,
> I do not agree with you. I believe the Guassian nature of the distribution
> gives equal expectation (density) in all directions, as evidenced by:
> nn=1000000;r=randn(1,nn)+j*randn(1,nn); hist(angle(r),1000)
> The same is not true for uniform distributions.
> -Aj

Oops! I missed the "n" on the end of "randn"--I thought it was
suggesting a uniform distribution, instead of Gaussian. I apologize.

Andrew

--
Andrew Ross
Industrial and Systems Engineering
Lehigh University, www.lehigh.edu/~inime/
Bethlehem, Pennsylvania, USA
remove all digits <=4 from my e-mail address to reply

Subject: Random vectors

From: AJ \"no z\" Johnson

Date: 2 Oct, 2003 05:35:44

Message: 12 of 14

"Andrew M. Ross" <amr54321@lehigh.edu> wrote in message
news:3F7AF694.8010506@lehigh.edu...
> AJ "no z" Johnson wrote:
> > "Andrew M. Ross" <amr54321@lehigh.edu> wrote in message
> > news:blc9e7$gm4@fidoii.CC.Lehigh.EDU...
> >
> >>Mauro Santos wrote:
> >>
> >>>Does anyone know how to generate random vectors as points on a
> >>>n-dimensional unit sphere?
> >>
> >>AJ "no z" Johnson wrote:
> >> > v = randn(1,n); % A vector with random "direction"
> >> > v = v./sqrt(v*v'); % Normalize (on unit sphere)
> >>
> >>Unfortunately, neither of these give a uniform distribution over the
> >>sphere. The first has more mass near the corners of the unit n-cube,
> >>[...]
> >>It's an interesting exercise, actually, to try the first suggestion in 2
> >>dimensions, and plot a histogram of the resulting angles. You should
> >>see four equal-size peaks.
> >>Andrew
> > Andrew,
> > I do not agree with you. I believe the Guassian nature of the
distribution
> > gives equal expectation (density) in all directions, as evidenced by:
> > nn=1000000;r=randn(1,nn)+j*randn(1,nn); hist(angle(r),1000)
> > The same is not true for uniform distributions.
> > -Aj
>
> Oops! I missed the "n" on the end of "randn"--I thought it was
> suggesting a uniform distribution, instead of Gaussian. I apologize.
>
> Andrew

I had tried that too, just for fun, and indeed it filled up the corners as
you predicted.
Cheers!
-Aj

Subject: Random vectors

From: Vinita

Date: 25 Jul, 2012 07:51:13

Message: 13 of 14

Hi Pierpa,

I have generated a unit sphere using this code :-

theta=linspace(0,2*pi,40);
phi=linspace(0,pi,40);
[theta,phi]=meshgrid(theta,phi);
rho=1;
x=rho*sin(phi).*cos(theta);
y=rho*sin(phi).*sin(theta);
z=rho*cos(phi);
mesh(x,y,z)
axis equal
grid on
ALPHA('clear'); % renders the sphere transparent

Now do you have any idea how to pick 4 random points from this sphere.
Please help

Pierpa <NOpbruttiSPAM@BUBUstat.cmu.CICCIOedu> wrote in message <eec4a33.7@WebX.raydaftYaTP>...
> You might find this book useful:
> - Mark E. Johnson (1987)
> "Multivariate Statistical Simulation:
> A Guide to Selecting and Generating
> Continuous Multivariate Distributions",
> John Wiley & Sons
>
>
> Anyway, in general, if you know the parametric definition of a
> surface, you can play around with that as in the following case:
>
>
> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
> % Uniform on a Moebius'string
> %
> r = 3; R = 10;
> np = 100; nr = 20;
> N = 10000;
>
>
> phi = linspace(0,2*pi,100);
> rho = linspace(-r,r,nr);
>
>
> [PHI,RHO] = meshgrid(phi,rho);
>
>
> % Parametric def.
> THETA = PHI/2;
> X = (R+RHO.*sin(THETA)).*cos(PHI);
> Y = (R+RHO.*sin(THETA)).*sin(PHI);
> Z = RHO.*cos(THETA);
>
>
> % Sample
> xi = rand(N,1);
> eta = rand(N,1);
> rho = (1-2*xi)*r;
> theta = pi*eta;
> for k=1:10
> theta = pi*eta+rho./R.*cos(theta);
> end
> x = (R+rho.*sin(theta)).*cos(2*theta);
> y = (R+rho.*sin(theta)).*sin(2*theta);
> z = rho.*cos(theta);
>
>
> plot3(x,y,z,'.')
> axis('equal')
> %
> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
>
>
> HTH
>
>
> --P

Subject: Random vectors

From: Bruno Luong

Date: 25 Jul, 2012 08:36:07

Message: 14 of 14

"Vinita " <rsingh2083@gmail.com> wrote in message <juo8hh$dte$1@newscl01ah.mathworks.com>...

>
> Now do you have any idea how to pick 4 random points from this sphere.
> Please help

Generate n random points on the sphere:

n = 1000;

xyz=randn(3,n);
xyz=bsxfun(@rdivide,xyz,sqrt(sum(xyz.^2,1)));

plot3(xyz(1,:),xyz(2,:),xyz(3,:),'.')
axis equal

% Bruno

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us