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;
CodePudding user response:
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)
...
CodePudding user response:
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.