如何在MATLAB中用直方图制作CDF

时间:2016-04-03 11:51:12

标签: matlab statistics histogram probability cdf

我正在尝试创建一个直方图并从中生成CDF的函数。 但是我不能在Matlab中使用cdfplot函数。

我将如何做到这一点?

这会产生输入直方图:

x = randn(1000,1);
nbins = 25;
h = histogram(x,nbins)

2 个答案:

答案 0 :(得分:2)

在对直方图进行标准化后,使用MATLAB的cumsum函数。

hNormalized = h.Values / sum(h.Values);
cdf = cumsum(hNormalized)

完整代码:

x = randn(1000,1);
nbins = 25;
h = histogram(x,nbins);
hNormalized = h.Values / sum(h.Values);
cdf = cumsum(hNormalized);

nBins较小的结果(nBins = 8):

hNormalized =

0.0210    0.0770    0.1930    0.2830    0.2580    0.1250    0.0370    0.0060

cdf =

0.0210    0.0980    0.2910    0.5740    0.8320    0.9570    0.9940    1.0000

答案 1 :(得分:2)

从数据创建累积分布的最直接方法是生成empirical CDFecdf可以直接执行此操作。默认情况下,这并不需要为数据集生成直方图:

x = randn(1000,1);
ecdf(x);

Empirical CDF

但是,如果您想要较低分辨率的CDF,可以使用'cdf' normalization选项直接使用histogram

x = randn(1000,1);
nbins = 25;
histogram(x,nbins,'Normalization','cdf');

CDF histogram option

您可能会发现'cumcount'选项也很有用。有关如何从这些函数中提取和使用输出的详细信息,请参阅ecdfhistogram的文档。