Changing Histogram to PDF

79 次查看(过去 30 天)
clear all
close all
clc
M=10000;
N=100000;
figure(1)
for ii=1:N
x =sqrt(12)*(rand(1,M) -0.5);
if ii==1
mx = mean (x)
sx2 = var(x)
subplot(2,1,1), plot(x)
subplot(2,1,2), hist(x,50)
end
y(ii) = sum(x)/sqrt(M);
end
mean (y)
var(y)
figure(2)
subplot(2,1,1), plot(y)
subplot(2,1,2), hist(y,50)
-------------------------------------------------
I want to make Gaussian PDF from this histogram.
Please help me!!!!!!

采纳的回答

Adam Danz
Adam Danz 2019-4-1
编辑:Steven Lord 2020-5-2
Use histogram instead of hist.
Then use histcounts along with the pdf option to get the pdf.
h = histogram(y,50);
p = histcounts(y,50,'Normalization','pdf');
% plot it
figure
binCenters = h.BinEdges + (h.BinWidth/2);
plot(binCenters(1:end-1), p, 'r-')
[SL: fixed typo]
  2 个评论
Steven Lord
Steven Lord 2019-4-3
Why not do it with histogram alone?
y = randn(1, 1e5);
h = histogram(y, 50, 'Normalization', 'pdf');
If you need it to be a smooth(er, depending on how many bins you have) curve, rather than bars:
ycoords = h.Values;
edgecoords = h.BinEdges;
xcoords = (edgecoords(1:end-1)+edgecoords(2:end))./2;
hold on
plot(xcoords, ycoords)
Adam Danz
Adam Danz 2020-5-2
As far as I can tell, the only difference is the pdf line can be plotted without first plotting the historgram bars if the histcounts method is used. If the histogram bars are desired, then using histogram() directly would be more efficient.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Distribution Plots 的更多信息

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by