Sampling a random normal to create a vector of truncated normals
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
the cyclist
2018-9-5
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);
更多回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Fortran with MATLAB 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!