本文介绍了从一系列正态分布的数字中选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从具有正态分布的范围(例如 0 到 1920 像素)中选择一个像素,但我不知道如何在 MatLab 中执行此操作.我知道我可以使用 normrnd() 来检索给定 musigma 的随机值,但是我如何将其应用于我的情况?

I need to choose one pixel from a range of, for example, 0 to 1920 pixels with the normal distribution, but I have no idea how to do this in MatLab. I know that I can use normrnd() to retrieve a random value given mu and sigma, but how do I apply this to my situation?

mu 可能是 500 像素,sigma 可能是 100 像素.

mu might be at 500 pixels and sigma 100 pixels, for example.

我目前的做法是这样

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 并在值高于或低于我的界限时切断.不知道这有多好,但它有效.

So I'm just using normrnd and cutting off if the value is higher or lower than my bounds. Don't know how good this is, but it works.

推荐答案

当您绑定正态分布(或以任何其他方式过滤其结果)时,它就不再是正态分布.但是,存在一个截断正态分布,它与您要查找的内容最接近.它有自己的一组属性,类似于正态分布如果边界远离均值并且方差很小.使用 Matlab,您可以使用:

The moment you bound a normal distribution (or filter its results in any other way) it is not a normal distribution anymore. However, there exists a truncated normal distribution which is the closest thing to what you are looking for. It has it's own set of properties which are similar to a normal distribution if the bounds are far away from the mean and you have a low variance. With Matlab you can make that with:

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 构建:

Building it from scratch for Octave:

您的自制提案存在的问题是,如果实现在界限之外,则将值设置为界限值.因此,边界值将被过度选择.一种不那么脏的方法是仅仅绘制一个新的值.我没有可用的 Octave,但应该可以这样做:

Your self-made proposal has the problem that if a realisation is outside the bound, you set the value to the bound value. Therefore the bound value will be overly proportional chosen. A less dirty way is instead to just draw a fresh value. I don't have a working Octave, but something like this should do it:

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

就像一个警告:如果实现不太可能在界限内,那么这可能会运行很长时间......

Just as a warning: If it is unlikely that a realisation will be within the bounds then this may run for a very long time...

这篇关于从一系列正态分布的数字中选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-21 12:53