Sampling a random normal to create a vector of truncated normals
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponse acceptée
the cyclist
le 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);
Plus de réponses (1)
Voir également
Catégories
En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!