带有matlab绑定的指数随机数

时间:2012-09-10 20:22:49

标签: matlab statistics exponential

我想使用指数随机数生成器(平坦危险函数)选择50到150之间的值。如何在matlab中实现内置指数随机数函数的边界?

2 个答案:

答案 0 :(得分:2)

快速的方法是比您需要的序列更长,并抛出超出所需范围的值。

dist = exprnd(100,1,1000);
%#  mean of 100  ---^  ^---^--- 1x1000 random numbers
dist(dist<50 | dist>150) = []; %# will be shorter than 1000

如果在修剪后没有足够的值,您可以重复并附加到矢量上,或者您想要这样做。

答案 1 :(得分:2)

exprandn使用rand(请参阅>> open exprnd.m),因此您可以通过反转流程并在所需范围[r1, r2]内统一采样来约束其输出。

sizeOut = [1, 1000]; % sample size
mu = 100; % parameter of exponential 
r1 = 50;  % lower bound
r2 = 150; % upper bound

r = exprndBounded(mu, sizeOut, r1, r2); % bounded output    

function r = exprndBounded(mu, sizeOut, r1, r2);

minE = exp(-r1/mu); 
maxE = exp(-r2/mu);

randBounded = minE + (maxE-minE).*rand(sizeOut);
r = -mu .* log(randBounded);

绘制的密度(使用非参数核估计器)对于20K样本看起来如下

enter image description here