Get bin edges from a histogram by index of the bin

911 views Asked by At

I have a matrix m and plot a histogram of the third column. I search for the peak in the first 100 bins and get the frequency as a and the index of the bin as b. Now I need the edges of the bin with index b. How can I get them?

nbins = 1000; 
histo = histogram(m(:,3),nbins,'Orientation','horizontal'); 
[a,b] = max(histo.Values(1:100))
1

There are 1 answers

0
Dev-iL On BEST ANSWER

I can think of two easy ways to do this:

function q41505566
m = randn(10000,5);
nBins = 1000;

% Option 1: using histcounts:
[N,E] = histcounts(m(:,3),nBins);
disp(E(find(N(1:100) == max(N(1:100)),1,'first')+[0 1])); % find() returns the left bin edge

% Option 2: using BinEdges:
histo = histogram(m(:,3),nBins,'Orientation','horizontal'); 
[a,b] = max(histo.Values(1:100));
disp(histo.BinEdges(b:b+1));

If you need an explanation for the "tricks" - please say so.