简体   繁体   中英

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.

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

when you write f=hist(x,bin); you assign the values of the histogram to f as a vector, as you saw. normalization such that the area under the curve is 1, is then just f./sum(f) ...

Note that hist is no longer recommended and has been replaced by histogram .

There are normalisation options as name-value pairs when creating the histogram. histogram(x,bin,'Normalization','pdf'); or 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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