4.75

4.8 | 12 ratings Rate this file 20 Downloads (last 30 days) File Size: 770 Bytes File ID: #9443
image thumbnail

Random Points in an n-Dimensional Hypersphere

by Roger Stafford

 

24 Dec 2005 (Updated 28 Dec 2005)

Randomly and uniformly distributes points throughout a hypersphere.

| Watch this File

File Information
Description

This creates a set of random points defined by Cartesian coordinates and uniformly distributed over the interior of an n-dimensional hypersphere of radius r with center at the origin.

  The 'randn' function is first used to create independent multivariate normally distributed sets of n random variables, each representing points in n-dimensional space. The incomplete gamma function, 'gammainc', is then used to map these points radially to the interior of an n-dimensional hypersphere of finite radius r in such a way as to be spatially uniformly distributed.

MATLAB release MATLAB 5.2 (R10)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (14)
28 Dec 2005 Peter (PB) Bodin

Simple, yet very clever solution.

29 Dec 2005 John D'Errico

I'll admit it took me a minute or so to verify this code. Its
mathematically elegant. Its efficient. It even works in the
special case of n = 1.

23 Jan 2006 Peter Nave

The compactness of the code is stunning.

23 Jun 2006 Lee Jones

elegant procedure, surprisingly

03 Apr 2007 Steve Lord

Very nice piece of code. I have just two small suggestions for enhancements to the M-file help:

1) Include the calling syntax early in the help. I will admit I sometimes forget the calling syntax for functions I don't use often; my first resort is to type "help <functionname>", either to refresh my memory or to copy and paste the syntax into my code. In a case like this, I can see users perhaps forgetting the order of the parameters.

2) Include an example. Having a example that users can copy and paste included in the help gives people who may not know how to use your function a "known good" state -- from there, they can tweak. For a straightforward function like this, it may not seem necessary, but I think it's a good habit. It could be as simple as the two lines of code to generate the sample image.

24 Sep 2007 Aree Witoelar

Thanks a lot for the elegant code! I had trouble generating this distribution. Before this, my solution was to sample and reject; but for high dimensions it was too crude and consuming.

31 Mar 2009 MathMoose  
15 Jan 2010 molc morloc  
05 Apr 2010 Florian

The elegancy is breathtaking! I wish you would post more functions!

01 Jul 2010 Jiaqi  
10 Aug 2010 chen ?

Thanks a lot!
I have another question.If I wanna to get a uniform distribution on the surface of the hypersphere (radius equals 1), whether is it correct to normalize the generated vectors to be length 1. Thank you very much!

11 Feb 2011 Laurent

The convenient use of the incomplete gamma function luckily provided by Matlab can be avoided, so as to make the function probably faster, more elegant and reliable. No non-elementary function is necessary here.

In the last line, simply replace
gammainc(s2/2,n/2)
by
rand(m,1)

This amount to sampling the radius randomly, independently of the point "X/sqrt(s2)" that is uniform on the surface of the sphere. Indeed, one can check that if u is uniform on [0,1] then r*u^(1/n) has same distribution as the norm of a vector chosen uniformly inside the ball B(0,r) in n-dimensional space.

I don't have matlab right here to check for speed compared to using gammainc, but in any case this method does not rely on some blackbox approximate integration, which makes it, to my taste, more elegant. It is also more portable into other programming languages.

28 Apr 2011 Vincent Dubourg

Hi!

Thank you for sharing this neat function! I was wondering if you could give me any reference explaining the theoretical concepts underlying this script?

Thanks,
Vincent

28 Apr 2011 Vincent Dubourg

Hi again,

I googled a little more and finally found a short explanation in the book by Rubinstein (2008, p. 69). I leave the link here in case it deserves someone else...
http://books.google.com/books?id=1-ffZVmazvwC&pg=PA69&lpg=PA69&dq=random+hypersphere+rubinstein&source=bl&ots=-q859Ytx3q&sig=q-PtG2P62_kTCgzi-J7QuqvPOZA&hl=fr&ei=zZ25TcfUJ4SChQeIzY3_Dg&sa=X&oi=book_result&ct=result&resnum=1&ved=0CBwQ6AEwAA

Cheers,
Vincent

Please login to add a comment or rating.
Tag Activity for this File
Tag Applied By Date/Time
ndimensional Roger Stafford 22 Oct 2008 08:10:41
hypersphere Roger Stafford 22 Oct 2008 08:10:41
random Roger Stafford 22 Oct 2008 08:10:41
incomplete Roger Stafford 22 Oct 2008 08:10:41
uniformlydistributed Roger Stafford 22 Oct 2008 08:10:41
radius chen ? 10 Aug 2010 10:58:15

Contact us at files@mathworks.com