簡體   English   中英

如何從 MATLAB 的 hist() 函數中標准化直方圖?

[英]How do you normalize a histogram from the hist() function of MATLAB?

我希望標准化我的直方圖,但由於某種原因,我的代碼中出現了一些錯誤。

N = 1000;
mu = 5; stdev = 2;
x = mu+stdev*randn(N,1);
bin=mu-6*stdev:0.5:mu+6*stdev;
f=hist(x,bin);
plot(bin,f,'bo');

counts = f.Values;
sum_counts = sum(counts);
width = f.BinWidth;

area = sum_counts*width;

我可以繪制直方圖,但在歸一化時出現錯誤。 我知道 histogram() 函數支持標准化,但我試圖避免這種情況。

Dot indexing is not supported for variables of this type.
     counts = f.Values;

當你寫f=hist(x,bin); 如您所見,您將直方圖的值作為向量分配給f 歸一化,使得曲線下的面積為 1,然后就是f./sum(f) ...

請注意,不再推薦使用hist ,並已將其替換為histogram

創建直方圖時,有標准化選項作為名稱-值對。 histogram(x,bin,'Normalization','pdf'); histogram(x,bin,'Normalization','probability'); ,例如,可能是您正在尋找的內容。 完整的規范化選項可以在doc中找到。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM