1

I wanted to generate a set of coordinates distributed uniformly at random within a ball of radius R. Is there any way to do this in Matlab without for loops, in a matrix-like form?

Thanks

UPDATE: I'm sorry for the confusion. I only need to generate n points uniformly at random over a circle of radius R, not a sphere.

Bob
  • 10,741
  • 27
  • 89
  • 143

4 Answers4

3

the correct answer is here http://mathworld.wolfram.com/DiskPointPicking.html. The distribution is known as "Disk point picking"

Bob
  • 10,741
  • 27
  • 89
  • 143
1

I was about to mark this as a duplicate of a previous question on generating uniform distribution of points in a sphere, but I think you deserve the benefit of doubt here, as although there's a matlab script in the question, most of that thread is python.

This little function given in the question (and I'm pasting it directly from there), is what you need.

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);

To learn why you can't just use uniform random variable for all three co-ordinates as one might think is the correct way, give this article a read.

Community
  • 1
  • 1
abcd
  • 41,765
  • 7
  • 81
  • 98
  • Your Wikipedia link doesn't seem applicable here. That article is discussing how to create a uniform distribution *on the surface of a sphere*, whereas here we are creating a uniform distribution *within a spherical volume*. – gnovice Apr 12 '11 at 02:45
  • @gnovice: I wasn't offering that as a solution, rather an explanation as to why an intuitive solution might not work. The arguments w.r.t. PDF/CDF are same for both cases. – abcd Apr 12 '11 at 04:06
  • @banana: I just saw your updated question. You can use the same script above for points on a circle. The syntax will be `randsphere(N,2,R)`, where `N` is the number of points, `R` is the radius of the circle. – abcd Apr 12 '11 at 04:57
1

For the sake of completeness, here is some MATLAB code for a point-culling solution. It generates a set of random points within a unit cube, removes points that are outside a unit sphere, and scales the coordinate points up to fill a sphere of radius 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

One key drawback to this point-culling method is that it makes it difficult to generate a specific number of points. For example, if you want to generate 1000 points within your sphere, how many do you have to create in the cube before culling them? If you scale up the number of points generated in the cube by a factor of 6/pi (i.e. the ratio of the volume of a unit cube to a unit sphere), then you can get close to the number of desired points in the sphere. However, since we're dealing with (pseudo)random numbers after all, we can never be absolutely certain we will generate enough points that fall in the sphere.

In short, if you want to generate a specific number of points, I'd try out one of the other solutions suggested. Otherwise, the point-culling solution is nice and simple.

gnovice
  • 125,304
  • 15
  • 256
  • 359
  • I thought about that too, but I wanted to generate exactly n points. worst case scenario I will do it with a for loop in c and call the function from matlab. But I am looking for a nicer solution – Bob Apr 12 '11 at 03:36
0

Not sure if I understand your question correctly, but can't you just generate any random number inside a sphere by setting φ, θ and r, assigned to random numbers?

Pedery
  • 3,632
  • 1
  • 27
  • 39
  • 3
    He wants it to be uniformly distributed. If you uniformly distribute those three variables, you'll find that the areas closer to the center have a denser distribution, i.e. not uniform. You could uniformly distribute on x, y, z and reject generated points that aren't in the sphere, but this would still require loops (or if you really must avoid loops, use recursion, which would be stupid here.) –  Apr 12 '11 at 01:00
  • Ah! I see! Then you probably want some distribution probability making it less probable to randomly pick the values that result in the density changing. Yet, are you sure the computational complexity outweighs a standard Monte Carlo method of simply throwing away the point if it's outside the sphere? – Pedery Apr 12 '11 at 01:34
  • Looks like the link http://stackoverflow.com/questions/5408276/ by yoda above gives you the answer on how to downdistribute the values. – Pedery Apr 12 '11 at 01:45
  • Personally, I'd use the stupid way because I understand the math. I'd probably mess up an equation somewhere if I tried implementing the normalized distribution. –  Apr 12 '11 at 02:24