Hi,
I need to generate uniformly distributed points inside an elliptical belt.
I know how to randomly generate uniform points inside an ellipse, but I need uniform points between two ellipses of different sizes, both centred at the origin.
Thanks!

 채택된 답변

John D'Errico
John D'Errico 2020년 11월 23일
편집: John D'Errico 2020년 11월 23일

0 개 추천

If you already know how to generate random points uniformly over an elliptical region, then you already have the simple answer.
  1. Generate uniform points inside the LARGER ellipse. You will want to oversample, thus taking more points than you really need.
  2. Discard all points inside the smaller ellipse. This test is simple if you know the equation of the smaller ellipse.
  3. If the size of set that remains is greater than the final number you want to achieve, then discard some of them randomly.
  4. If the set that remains is smaller than the final number you want to achieve, then return to step 1 to generate more points to fill out the set.
This "rejection" scheme is sometimes the best and most efficient eay to generate points that are randomly distributed over a domain. It is good as long as the rejected fraction will not be too large. Since the original sampling scheme is RELATIVELY easy to generate points inside an ellipse, it is not difficult to oversample. And the test is also simple to identify points inside an ellipse.
As such, the rejection scheme as described is simple and will often be quite efficient. The only case where this becomes a problem is when the two ellipses are almost identically the same size and shape. In that case, you have other options, but they will be more intensive in terms of the code you must write, but they will also require additional computations, making the oversampling/rejection scheme best unless the rejected fraction is quite large. So I won't spend the time to discuss your alternative options at length unless there is a good reason.
The main alternative option would be to modify the scheme whereby you generate points inside the larger ellipse. The modification is not that extensive, but would require a fair amount of explanation on my part.

댓글 수: 4

Gaelle Rabarison
Gaelle Rabarison 2020년 11월 23일
편집: Gaelle Rabarison 2020년 11월 23일
Thanks! For the problem I am looking at, the two ellipses are of different sizes, which means the solution you proposed should work.
Here is a simple example using the rejection method
a = 1;
b = 2;
r1 = 1;
r2 = 1.5;
r_max = max(r1, r2);
a_ = a*r_max;
b_ = b*r_max;
x = rand(10000,1)*2*a_-a_;
y = rand(10000,1)*2*b_-b_;
idx = ((x.^2/a^2+y.^2/b^2)>r1^2) & ((x.^2/a^2+y.^2/b^2)<r2^2);
xv = x(idx);
yv = y(idx);
plot(xv, yv, '+')
Gaelle Rabarison
Gaelle Rabarison 2020년 11월 23일
Perfect, thanks!
John D'Errico
John D'Errico 2020년 11월 23일
Excellent. As I said, rejection is often a great way to generate uniformly distributed samples.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Bruno Luong
Bruno Luong 2020년 11월 23일
편집: Bruno Luong 2020년 11월 23일

0 개 추천

If the two ellipse has the same aspect ratio and aligned then this is the method without rejection.
n=1e4;
ax=3;
ay=1;
rint=1;
rext=2;
% "Straight" ellipse bell
% B = { (x,y) in R^2 st
% (x/axext)+^2 + (y/ayext)+^2 < 1 &
% (x/axint)+^2 + (y/ayint)+^2 > 1
% ellipses have the same aspect
% axint = ax*rint; ayint = ay*rint;
% axext = ax*rext; ayext = ay*rext;
% generate n points in B
xy=randn(2,n);
rxy=sqrt(sum(xy.^2,1));
r=rint^2+rand(1,size(xy,2))*(rext^2-rint^2);
xy=xy.*(r.^(1/2)./rxy);
xy=[ax;ay].*xy;
x=xy(1,:);
y=xy(2,:);
plot(x,y,'.')
axis equal
And it works just fine on thin-bell (rext=1.05)
or supper thin rext=1.001

댓글 수: 1

Gaelle Rabarison
Gaelle Rabarison 2020년 11월 23일
Thank you, this is a nice alternative to rejection sampling.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Least Squares에 대해 자세히 알아보기

질문:

2020년 11월 23일

댓글:

2020년 11월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by