How to merge very close bins in histogram/hiscounts
6 次查看(过去 30 天)
显示 更早的评论
I have such a histogram ( saved as the *.mat file and attached), and I would like to "shapren" it by merging counts from very close bins (circled) so eventually there should be multiple isolated but taller single bins. I can think of the method by setting threshold of "very close bins", calculating bin-bin distance, grouping neighbour bins, and merging same-group bins together. But is there any more flexible/dynamic way to do it without setting a fixed threshold?
0 个评论
采纳的回答
Jan
2022-11-15
编辑:Jan
2022-11-15
[b, n] = RunLength(x);
match = (b == 0 & n > 5);
Lim = b(match) + n(match) / 2;
Now the limits are the centers of the blocks with a width of at least 5. Use this as bins for histcounts.
Maybe you have to care about the margins, if the data do not start with empty blocks.
If you do not have a C-compiler, use RunLength_M from the same submission.
更多回答(1 个)
Steven Lord
2022-11-15
Once you've binned your data once, use whatever mechanism you want to determine which bin edges separate bins you want to merge. After you've removed those interior edges from the list of edges, set the histogram's BinEdges property to the pruned list and it will rebin the data.
x = randn(1, 1e5);
h1 = histogram(x, 12); % 12 bins
Let's delete every 4th bin edge. In order for both histograms to show up in this Answers post I need to create a new histogram, but if you wanted to update the existing one you'd use the commented out command.
E = h1.BinEdges;
E(4:4:end) = [];
figure
h2 = histogram(x, E); % or
% h1.BinEdges = E;
Let's check the bin counts and bin edges.
E1 = h1.BinEdges.';
result1 = table(h1.BinCounts.', [E1(1:end-1), E1(2:end)], 'VariableNames', ["Counts", "Bins"])
E2 = h2.BinEdges.';
result2 = table(h2.BinCounts.', [E2(1:end-1), E2(2:end)], 'VariableNames', ["Counts", "Bins"])
另请参阅
类别
在 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!