Changing Histogram to PDF

84 ビュー (過去 30 日間)
Hosin Lee
Hosin Lee 2019 年 4 月 1 日
編集済み: Steven Lord 2020 年 5 月 2 日
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 Center および File ExchangeData Distribution Plots についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by