在matlab中规范化直方图

时间:2014-10-03 14:52:11

标签: matlab

我有直方图

hist(A, 801)

目前类似于正常曲线但在y = 1500时具有最大值,在x = 0.5时具有最大值。我想将它标准化,所以我尝试了

h = hist(A, 801)
h = h ./ sum(h)
bar(h)

现在我得到一条正常的曲线,最大值为y = .03,但平均值为x = 450。

如何降低频率,使总和为1,同时保持相同的x范围?

A源自

A = walk(50000, 800, .05, 2, .25, 0)

,其中

function [X_new] = walk(N_sim, N, mu, T, sigma, X_init)

delt = T/N;
up = sigma*sqrt(delt);
down = -sigma*sqrt(delt);

p = 1./2.*(1.+mu/sigma*sqrt(delt));

X_new = zeros(N_sim,1);
X_new(1:N_sim,1) = X_init;

ptest = zeros(N_sim,1);

for i = 1:N

    ptest(:,1) = rand(N_sim,1);
    ptest(:,1) = (ptest(:,1) <= p);

    X_new(:,1) = X_new(:,1) + ptest(:,1)*up + (1.-ptest(:,1))*down;

end

1 个答案:

答案 0 :(得分:2)

现在代码的总和是1。

您可能希望积分等于1(这样您就可以与理论上的pdf进行比较)。在那种情况下:

[h, c] = hist(A, 801); %// c contains bin centers. They are equally spaced
h = h / sum(h) / (c(2)-c(1)); %// normalize to area 1
trapz(c,h) %// compute integral. Should be approximately 1