09-22-2003, 07:18 AM | #1 (permalink) |
Psycho
|
Help with random numbers in Matlab
Does anyone know how to generate a matrix or sequence of random numbers in Matlab? The numbers have to fall inside a ellipse of equation:
t=[0:2*pi/100:2*pi]; x=5*cos(t); y=1*sin(t); plot(x,y,'.') or any other ellipse/shape. The end result would be an ellipse filled with random points. I've tried looking for a command that would generate points within a constraint but to no avail. |
09-22-2003, 10:30 AM | #2 (permalink) |
Riiiiight........
|
One method, called Acceptance/Rejectance or Thinning,
is to bound your ellipse by a box. Generate random points within this box, then check if the points are within the ellipse. Throw away points that are not in the ellipse. So the better you "bound" your shape, the less points you have to throw away. In fact, since your ellipse is symmetrical about the x-axis, you can simply use 'half' and ellipse, the part thats above the x-axis. |
09-22-2003, 02:13 PM | #3 (permalink) |
undead
Location: nihilistic freedom
|
This method kinda depends on the granularity of your values... but, you could generate a list of all points uniformly distributed within your elipse. Then randomize this list. Now for your generator, all you need to do is traverse the list. Or I guess you could just walk around the list with a large prime number... same effect.
|
09-22-2003, 03:11 PM | #4 (permalink) |
Psycho
|
Yeah, I had already started to write a subroutine that would generate a random matrix then delete the points that don't fall inside the boundary of the ellipse. But I was hoping to find some obscure Matlab command that would make it much easier. These ideas are pretty good though.
btw, moelester: what do you mean by "walk around the list with a large prime number"
__________________
"Empirically observed covariation is a necessary but not sufficient condition for causality" - Edward Tufte |
09-23-2003, 06:05 AM | #5 (permalink) |
Riiiiight........
|
Well, there are several ways of generating random numbers corresponding to a distribution from uniform[0,1] distributions ( thats what you essentially get when you use rand().)
Another way, if you have a nice function, is to use the inverse of the cumulative distribution function. Since the cumulative distribution function maps from [range of values in the distribution] to [0,1], and thus the inverse maps from [0,1] to [range of values], and the uniform is also from [0,1], you can get generate a random U(0,1) number, and plug it into the inverse of the CDF. For more information about generating random numbers, check up a book on simulation. The textbook by Law and Kelton has a pretty comprehensive section on how to generate random numbers. |
Tags |
matlab, numbers, random |
|
|