No BSD License  

Highlights from
Restricted sampling from Gaussian Distribution

from Restricted sampling from Gaussian Distribution by Kanchi
Sample x from N(x_mu, x_var), restricted in x_min<=x<=x_max.

func_restricted_sampling(x_mu, x_var, x_min, x_max);
function x= func_restricted_sampling(x_mu, x_var, x_min, x_max);
% Matlab implementation of restricted sampling from Gaussian distribution
%
% Objective: sample x (column vector) from N(x_mu, x_var), restricted in
% x_min<=x<=x_max.
%
% input:    x_mu, x_var: the parameter of the pdf of x
%           x_min, x_max: the range of x
% output:   x: the sample
% 
% Acknowledge
% Peter J. Acklam's toolbox for the inverse normal cumulative distribution function
% http://home.online.no/~pjacklam/notes/invnorm/index.html
%
%
% Jing Tian
% Contact me : scuteejtian@hotmail.com
% This program is written in Oct.2004 during my postgraduate studying in 
% NTU, Singapore.

r =  rand(size(x_mu, 1), 1);
y_min = (x_min - x_mu) ./ sqrt(x_var);
y_max = (x_max - x_mu) ./ sqrt(x_var);

nerfcMin = ltpnorm(y_min);
nerfcMax = ltpnorm(y_max);
temp = nerfcMin + (nerfcMax-nerfcMin) .* r;
x = ltqnorm(temp) .* sqrt(x_var) + x_mu;

Contact us at files@mathworks.com