How to merge very close bins in histogram/hiscounts
    조회 수: 4 (최근 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에서 Histograms에 대해 자세히 알아보기
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

