Accessing frequencies of arbitrarily selected histogram bins
12 次查看(过去 30 天)
显示 更早的评论
I would like to access the frequencies of arbitrarily selected bins on my histogram. As in, say I want to sum up only the frequencies of bins 3 to 7. Or if I'd like to sum up frequencies only for the bins that have frequencies larger than a certain value. And so on.
I'm reading on histogram and histcount docs but I'm struggling on how to apply it to my problem. An old thread on Ask the Community discussing something similar suggests to bypass the histogram completely and do this on the raw data. Still, I'm wondering if there is a way to do it using histograms as I already have a bunch of them.
Thank you.
0 个评论
采纳的回答
Steven Lord
2022-2-9
Let's take a look at a sample histogram. I'm using rng default to create the sample data so the histogram created when you run this code exactly matches the one I create by running the code here in Answers.
rng default
x = randn(1, 100);
h = histogram(x)
yline(32, 'r:')
The Values properties are of particular interest. These are the actual counts (in this case, since the Normalization property is set to 'count'). How many bins contain more than 5 values?
nnz(h.Values > 5)
Which bin is the highest and what are the edges of that bin?
[maxHeight, maxLocation] = max(h.Values)
% Value(n) counts data falling between BinEdges(n) and BinEdges(n+1)
edgesOfMaxBin = h.BinEdges(maxLocation + [0, 1])
You can confirm by looking at the picture that this is correct. The red dotted line is at y = 32 and the top of the bin representing [0, 1) exactly touches that dotted line.
If you don't want the picture you can do these same types of operations on the outputs of histcounts. Some of the properties of the histogram object aren't returned by histcounts and so if you want them you'd need to compute them.
[theValues, theEdges] = histcounts(x)
theBinWidth = diff(theEdges) % Not returned by histcounts, but easy to compute
theNumBins = numel(theValues) % Ditto
Instead of h.Values use theValues and instead of h.BinEdges use theEdges.
0 个评论
更多回答(2 个)
Paul
2022-2-9
The bin values are stored as propreties of the histogram object, which can be returned from histogram.
rng(101)
x = rand(20,1);
h = histogram(x);
h.Values
Or can be obtained from the histogram figure if its already created
h1 = get(gca,'Children');
h1.Values
1 个评论
Steven Lord
2022-2-9
Instead of getting all the axes Children, which may give you more than you expected, I'd use findobj or findall to specifically find the histogram handle.
rng default
x = randn(1, 100);
h = histogram(x);
yline(32, 'r:')
C = get(gca, 'Children')
h2 = findobj(gca, 'Type', 'Histogram')
h == C % [false; true]
h == h2 % true
Milos Krsmanovic
2022-2-9
2 个评论
Steven Lord
2022-2-9
If you've called histogram you don't need to also call histcounts. Your first example is more easily done with logical indexing
rng default
x = randn(1, 100);
h = histogram(x);
sum(h.Values(h.Values > 5))
Your second can use linear indexing.
nbins = numel(h.Values)
valuesOfOddIndexBins = h.Values(1:2:nbins)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Distribution Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!