Sampling a random normal to create a vector of truncated normals
2 views (last 30 days)
Show older comments
Is there a way in matlab as there is in Fortran to draw samples from normrnd with specified mean and sd and reject any that are less than a specified lower bound or greater than an upper bound. In Fortran you can simply set a zero flag for each random that is outside the limits, draw another random and if it passes the limits test add it to a vector. How can I do this in Matlab?
0 Comments
Accepted Answer
the cyclist
on 5 Sep 2018
Less elegant than truncate, but can be used in core MATLAB ...
% Desired number of samples
N = 1000;
% Draw some extra, because of the rejection. Need to adjust this to be sure to get enough extra. Will depend on how severe the truncation is.
N_extra = 100;
% Draw from normal
x = randn(N+N_extra,1);
% Reject values that lie outside truncated region
x(x<-2 | x>2) = [];
% Trim to the desired number of samples
x = x(1:N);
More Answers (1)
See Also
Categories
Find more on Fortran with MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!