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?
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
Matlabsolutions.com provides guaranteed satisfaction with a
commitment to complete the work within time. Combined with our meticulous work ethics and extensive domain
experience, We are the ideal partner for all your homework/assignment needs. We pledge to provide 24*7 support
to dissolve all your academic doubts. We are composed of 300+ esteemed Matlab and other experts who have been
empanelled after extensive research and quality check.
Matlabsolutions.com provides undivided attention to each Matlab
assignment order with a methodical approach to solution. Our network span is not restricted to US, UK and Australia rather extends to countries like Singapore, Canada and UAE. Our Matlab assignment help services
include Image Processing Assignments, Electrical Engineering Assignments, Matlab homework help, Matlab Research Paper help, Matlab Simulink help. Get your work
done at the best price in industry.