在Matlab中产生偏向边界的随机数 [英] Generating random numbers in matlab biased towards the boundaries

查看:162
本文介绍了在Matlab中产生偏向边界的随机数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Matlab中生成有偏的随机数.让我再解释一下,我的意思是偏见. 可以说我分别定义了30和10的上限和下限. 我想生成偏向边界的N个随机数,这样,与位于中间某个位置的数字相比,靠近10和30(极值)的数字的可能性更大. 我怎样才能做到这一点? 任何帮助都非常感激:)

I want to generate biased random numbers in matlab. Let me explain a bit more, by what I mean by biased. Lets say I have a defined upper bound and lower bound of 30 and 10 respectively. I want to generate N random numbers biased towards the bounds, such that the probability of the numbers lying close to 10 and 30 (the extremes) is more as compared to them lying some where in the middle. How can I do this? Any help is much appreciated :)

推荐答案

% Upper bound
UB = 30
% Lower bound
LB = 0;
% Range
L = UB-LB;
% Std dev - you may want to relate it to L - maybe use sigma=sqrt(L)
sigma = L/6;
% Number of samples to generate
n = 1000000;
X = sigma*randn(1,n);
% Remove items that are above bounds - not sure if it's what you want.. if not comment the two following lines
X(X<-L) = [];
X(X>L) = [];

% Take values above zero for lower bounds, other values for upper bound
ii = X > 0;
X(ii) = LB + X(ii);
X(~ii) = UB + X(~ii);

% plot histogram
hist(X, 100);

我在这里使用了正态分布,但显然您可以适应使用其他分布..您也可以更改sigma.

I used a normal distribution here but obviously you can adapt to use others.. you can change the sigma also.

这篇关于在Matlab中产生偏向边界的随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆