필터 지우기
필터 지우기

Rejection sampling on uniform distribution?

조회 수: 1 (최근 30일)
matlabkid602
matlabkid602 2018년 2월 18일
댓글: Image Analyst 2018년 2월 18일
How can I use matlab to perform rejection sampling on a uniform distribution using rand, to pick 20% of samples in the range 0 to 0.2 and the other 80% between 0.2 and 1?

답변 (1개)

Image Analyst
Image Analyst 2018년 2월 18일
Try this:
numPoints = 1000 % Total number of points to save.
numLow = 1;
numHigh = 1;
% Start collecting elements using acceptance if in range,
% and rejection if out of range.
for k = 1 : 10000000 % enough to make sure we will collect at least numPoints elements.
r = rand();
% Save the number if it's in the low range.
if r <= 0.2 && numLow <= 0.20 * numPoints
% In range. Store/save the number.
lowKeepers(numLow) = r;
numLow = numLow + 1;
else
% Not in range so reject/ignore it.
end
% Save the number if it's in the high range.
if r >= 0.8 && numHigh <= 0.80 * numPoints
% In range. Store/save the number.
highKeepers(numHigh) = r;
numHigh = numHigh + 1;
else
% Not in range so reject/ignore it.
end
end
  댓글 수: 2
matlabkid602
matlabkid602 2018년 2월 18일
Thanks for your answer! It works for the first bit, but I need 80% of the numbers to lie in the range .2-1. Just now it produces only numbers 0 to .2 and .8 to 1. See image. How can I change this?
Image Analyst
Image Analyst 2018년 2월 18일
Sorry, make this change:
if r >= 0.2 && numHigh <= 0.80 * numPoints

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

제품

Community Treasure Hunt

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

Start Hunting!

Translated by