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 で Creating and Concatenating Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!