Changing Histogram to PDF

83 visualizaciones (últimos 30 días)
Hosin Lee
Hosin Lee el 1 de Abr. de 2019
Editada: Steven Lord el 2 de Mayo de 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!!!!!!

Respuesta aceptada

Adam Danz
Adam Danz el 1 de Abr. de 2019
Editada: Steven Lord el 2 de Mayo de 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 comentarios
Steven Lord
Steven Lord el 3 de Abr. de 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 el 2 de Mayo de 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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Data Distribution Plots en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by