简体   繁体   English

如何从 MATLAB 的 hist() 函数中标准化直方图?

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

I wish to normalize my histogram, but for some reason I get some error in my code.我希望标准化我的直方图,但由于某种原因,我的代码中出现了一些错误。

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;

I get to plot my histogram but I get an error in normalization.我可以绘制直方图,但在归一化时出现错误。 I know that the histogram() function supports normalization but I am trying to avoid that.我知道 histogram() 函数支持标准化,但我试图避免这种情况。

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

when you write f=hist(x,bin);当你写f=hist(x,bin); you assign the values of the histogram to f as a vector, as you saw.如您所见,您将直方图的值作为向量分配给f normalization such that the area under the curve is 1, is then just f./sum(f) ...归一化,使得曲线下的面积为 1,然后就是f./sum(f) ...

Note that hist is no longer recommended and has been replaced by histogram .请注意,不再推荐使用hist ,并已将其替换为histogram

There are normalisation options as name-value pairs when creating the histogram.创建直方图时,有标准化选项作为名称-值对。 histogram(x,bin,'Normalization','pdf'); or histogram(x,bin,'Normalization','probability');histogram(x,bin,'Normalization','probability'); , for example, may be what you are looking for. ,例如,可能是您正在寻找的内容。 The full range of normalisation options can be found in the doc .完整的规范化选项可以在doc中找到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM