
Changing Histogram to PDF
84 ビュー (過去 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!!!!!!
0 件のコメント
採用された回答
Adam Danz
2019 年 4 月 1 日
編集済み: Steven Lord
2020 年 5 月 2 日
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
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
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 Center および File Exchange で Data Distribution Plots についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!