Plot occurrences of values with bin edges, by using "histcounts" and "plot" functions
17 views (last 30 days)
Show older comments
Hi, I have 1000 normally distributed random numbers (with mean = 3 and standard deviation = 10):
X = normrnd(3,10,[1,1000])
I want to partition the X values into 100 bins, and return the count in each bin (i.e. the occurrence of values within each bin), as well as the bin edges:
[n,bin_edges] = histcounts(X,100);
Then, I want to plot the count in each bin together with the values of the bin edges.... Should I (maybe) use the average value of two edges, i.e. the, or is there any other way to plot the count of my values with the bin edges?
plot(bin_edges(1:100),n); % this is not correct, because I plot the left edge for each occurrence of values
0 Comments
Accepted Answer
Steven Lord
on 8 Mar 2022
Why not just use histogram?
X = normrnd(3,10,[1,1000]);
h = histogram(X, 100);
c = h.BinCounts;
e = h.BinEdges;
for whichBin = 1:5
fprintf("In bin %d [%g, %g) there were %d elements.\n", ...
whichBin, e(whichBin:whichBin+1), c(whichBin));
end
More Answers (0)
See Also
Categories
Find more on Annotations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!