如何在 MatLab 中绘制概率密度函数? [英] How to draw probability density function in MatLab?

查看:141
本文介绍了如何在 MatLab 中绘制概率密度函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

x = [1 2 3 3 4]cdfplot(x)

谷歌搜索后,我发现上面的代码会在Matlab中为我绘制一个累积分布函数.
有没有简单的方法可以画出概率密度函数?

澄清.我需要一个具有均匀分布的 x 轴的图.我希望它看起来不像条形图.(我会有数百万个整数)
不好意思,又来更新了.我的数据是整数,但实际上它们代表时间(我期望在完全相同的值下有几个相当高的峰值,而其他值应该看起来好像它们不是离散的).我实际上开始怀疑这是否本质上不是离散整数.CDF 肯定会工作,但是当谈到 PDF 时,它似乎比我预期的要复杂.

解决方案

您可以使用函数 像这样的函数产生与上面相同的图形:

data = [1 2 3 3 4];N = histcounts(data, 'BinLimits', [0 10], 'BinMethod', 'integers', 'Normalization', 'pdf');情节(N);xlabel('整数值');ylabel('概率');

x = [1 2 3 3 4]
cdfplot(x)

After Googling, I find the above code will draw a cumulative distribution function for me in Matlab.
Is there a simple way to draw a probability density function?

To Clarify. I need a graph that has an evenly distributed x-axis. And I would prefer it does not look like a bar graph. (I would have millions of integers)
Sorry, update again. My data are integers, but actually they represents time(I expect several quite high peak at exact same value while other value should look like as if they are not discrete). I'm actually starting to wonder if this is actually not discrete integers inherently. CDF would definitely work, but when coming to PDF, it seems it's more complicated than I anticipated.

解决方案

You can generate a discrete probability distribution for your integers using the function hist:

data = [1 2 3 3 4];           %# Sample data
xRange = 0:10;                %# Range of integers to compute a probability for
N = hist(data,xRange);        %# Bin the data
plot(xRange,N./numel(data));  %# Plot the probabilities for each integer
xlabel('Integer value');
ylabel('Probability');

And here's the resulting plot:


UPDATE:

In newer versions of MATLAB the hist function is no longer recommended. Instead, you can use the histcounts function like so to produce the same figure as above:

data = [1 2 3 3 4];
N = histcounts(data, 'BinLimits', [0 10], 'BinMethod', 'integers', 'Normalization', 'pdf');
plot(N);
xlabel('Integer value');
ylabel('Probability');

这篇关于如何在 MatLab 中绘制概率密度函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆