从一系列正态分布的数字中选择
Choosing from a range of numbers with normal distribution
我需要从正态分布的范围内选择一个像素,例如 0 到 1920 像素,但我不知道如何在 MatLab 中执行此操作。我知道我可以使用 normrnd()
来检索给定 mu
和 sigma
的随机值,但是如何将其应用到我的情况中?
例如,mu
可能为 500 像素,sigma
可能为 100 像素。
我目前的做法是这样的
function xpos = apply_normal_distribution(mu, sigma, min_xpos=1, max_xpos=1920)
% Applies normal distribution with median mu and standard deviation sigma
% xpos will always be: min <= xpos <= max
xpos = ceil(normrnd(mu, sigma));
if xpos > max_xpos
xpos = max_xpos;
elseif xpos < min_xpos
xpos = min_xpos;
endif
end
所以我只使用 normrnd
并在值高于或低于我的界限时切断。不知道这有多好,但它确实有效。
当您绑定正态分布(或以任何其他方式过滤其结果)时,它就不再是正态分布了。但是,存在一个 truncated normal distribution ,它最接近您要查找的内容。它有自己的一组属性,类似于正态分布 如果 边界远离均值并且方差很小。使用 Matlab,您可以使用:
mu = 500;
sigma = 100;
%truncate at 0 and 1920
pd = truncate(makedist('Normal',mu,sigma),0,1920);
% take some (10) samples
samples = random(pd,10,1);
从头开始为 Octave 构建它:
你的自制方案有一个问题,如果一个实现在边界之外,你将值设置为边界值。因此,绑定值将被过度选择。一种不那么肮脏的方法是只绘制一个新值。我没有可用的 Octave,但应该可以这样做:
function xpos = apply_normal_distribution(mu, sigma, min_xpos=1, max_xpos=1920)
% new realisations are drawn as long as they are outside the bounds.
while xpos<min_xpos | xpos>max_xpos
xpos = ceil(normrnd(mu, sigma));
end
end
作为一个警告:如果实现不太可能在范围内,那么这可能 运行 很长一段时间...
我需要从正态分布的范围内选择一个像素,例如 0 到 1920 像素,但我不知道如何在 MatLab 中执行此操作。我知道我可以使用 normrnd()
来检索给定 mu
和 sigma
的随机值,但是如何将其应用到我的情况中?
mu
可能为 500 像素,sigma
可能为 100 像素。
我目前的做法是这样的
function xpos = apply_normal_distribution(mu, sigma, min_xpos=1, max_xpos=1920)
% Applies normal distribution with median mu and standard deviation sigma
% xpos will always be: min <= xpos <= max
xpos = ceil(normrnd(mu, sigma));
if xpos > max_xpos
xpos = max_xpos;
elseif xpos < min_xpos
xpos = min_xpos;
endif
end
所以我只使用 normrnd
并在值高于或低于我的界限时切断。不知道这有多好,但它确实有效。
当您绑定正态分布(或以任何其他方式过滤其结果)时,它就不再是正态分布了。但是,存在一个 truncated normal distribution ,它最接近您要查找的内容。它有自己的一组属性,类似于正态分布 如果 边界远离均值并且方差很小。使用 Matlab,您可以使用:
mu = 500;
sigma = 100;
%truncate at 0 and 1920
pd = truncate(makedist('Normal',mu,sigma),0,1920);
% take some (10) samples
samples = random(pd,10,1);
从头开始为 Octave 构建它:
你的自制方案有一个问题,如果一个实现在边界之外,你将值设置为边界值。因此,绑定值将被过度选择。一种不那么肮脏的方法是只绘制一个新值。我没有可用的 Octave,但应该可以这样做:
function xpos = apply_normal_distribution(mu, sigma, min_xpos=1, max_xpos=1920)
% new realisations are drawn as long as they are outside the bounds.
while xpos<min_xpos | xpos>max_xpos
xpos = ceil(normrnd(mu, sigma));
end
end
作为一个警告:如果实现不太可能在范围内,那么这可能 运行 很长一段时间...