天天看點

Matlab生成歸一化直方圖

使用matlab的函數histogram可以直接得到資料的直方圖,但這并不是歸一化的直方圖。

使用如下代碼可以得到歸一化的直方圖

x = randn(, );
numOfBins = ;
[histFreq, histXout] = hist(x, numOfBins);
binWidth = histXout()-histXout();
figure;
bar(histXout, histFreq/binWidth/sum(histFreq));       
xlabel('x');
ylabel('PDF: f(x)');
hold on
% fit a normal dist to check the pdf
PD = fitdist(x, 'normal');
plot(histXout, pdf(PD, histXout), 'r');
           

結果示意:

Matlab生成歸一化直方圖

繼續閱讀