Generating Random (x y z) Coordinates Constrained by Euclidean Distance

16 views (last 30 days)
I would like to write a function to generate randomized xyz coordinates from a set starting coordinates where the Euclidean distance from each original coordinate to its respective new, randomized coordinate would be no greater than 1.5 mm. I would like this process to repeat as many times as I specify (m = # of repetitions). More specifically, the function would take as input an nx3 matrix of xyz coordinates (each row is a separate observation, columns 1, 2, and 3 are x-, y-, and z-coordinates respectively), and outputs an m x 1 cell, where each element of the cell contains an nx3 matrix of the randomized xyz coordinates.
I can think of an ugly way to do this with a while loop and if/else statements, but I was wondering if there was a clean way to do this within the statistics toolbox. I want to use a uniform distribution to generate these numbers.
Thanks! LH

Accepted Answer

Walter Roberson
Walter Roberson on 1 May 2012
I don't think you will find this in the stats toolbox.
The process is equivalent to finding points within a sphere of given radius.
If I recall correctly, in the File Exchange, Roger Stafford has a contribution for finding points on a sphere; perhaps that could be adapted.
  2 Comments
Layla
Layla on 1 May 2012
This worked perfectly for me. Thank you all for your responses, I'm sure I'll make use of them in other codes.
Richard Brown
Richard Brown on 1 May 2012
This came up recently (computing random points within a sphere of fixed radius). See James Tursa's answer in this thread
<http://www.mathworks.com/matlabcentral/answers/35284-how-to-create-a-solid-spherical-cluster-with-random-distribution-of-points>

Sign in to comment.

More Answers (2)

Sean de Wolski
Sean de Wolski on 1 May 2012
Let's assume for this example that you want three points 1.5 away from the origin:
goal = 1.5; %our goal, fixed at 1.5 to demonstrate.
gs = goal^2; %goal squared
x = sqrt(randfixedsum(3,1,gs,0,gs)); %three numbers that sum to the goal squared
check:
sqrt(sum(x.^2))
randfixedsum() is available here.
For your real application, you'll have to generate a random distance between 0 and your max, and you'll have to do this n times as well as account for the fact that your first coordinate is likely not at the origin.

John D'Errico
John D'Errico on 1 May 2012
If you really want maximally distant points in that volume, this will be an NP-difficult problem. (Ok, I'm not at all sure what class it falls in, but the point is it will take some work to solve for the optimal solution.)
Solving for a uniformly random set is far easier of course. Others have suggested the nice utility by Roger Stafford, randfixedsum. You might choose to use that utility to oversample by a bit, then eliminate those points which fall most closely together. This will at least give you a viable solution that is computable in a reasonable time, though not at all truly optimal.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!