如何在matlab中绘制概率密度函数?

时间:2013-02-12 09:36:51

标签: matlab distribution uniform

我需要从

绘制均匀分布矩阵的概率密度函数
U = rand (1,1000)

但我不能使用ksdensity函数。我试过这个:

term = 1000;
U = rand (1,term);
x=0:0.001:1;
for j = 2:term;
    u_height(j) = u_height(j-1)+((abs(x(j)-U(j))<0.01/2)/0.01)/term;
    n_height(j) = n_height(j-1)+((abs(x(j)-N(j))<0.01/2)/0.01)/term;
end

但它没有正确绘图

1 个答案:

答案 0 :(得分:2)

函数ksdensity应该可以正常工作。您需要指定PDF的范围是有限的,并使用盒内核。

u = rand(10000,1);

ksdensity(u, 'Support', [0 1], 'kernel', 'box');

此外,您可以使用histc

来估算pdf
u = rand(10000,1);
bins = 0:.05:1;
counts = hist(u, bins);
p = counts ./ trapz(bins, counts);

plot(bins, p);
相关问题