Changing Histogram to PDF

84 Ansichten (letzte 30 Tage)
Hosin Lee
Hosin Lee am 1 Apr. 2019
Bearbeitet: Steven Lord am 2 Mai 2020
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!!!!!!

Akzeptierte Antwort

Adam Danz
Adam Danz am 1 Apr. 2019
Bearbeitet: Steven Lord am 2 Mai 2020
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 Kommentare
Steven Lord
Steven Lord am 3 Apr. 2019
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 am 2 Mai 2020
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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Data Distribution Plots finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by