0

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))
Dev-iL
  • 23,742
  • 7
  • 57
  • 99
Chaostante
  • 37
  • 6
  • 1
    Use the outputs of `histcounts`, or `histo.BinEdges`. – Dev-iL Jan 06 '17 at 12:19
  • Thank you! :D When I write `c = histo.BinEdges(b)` I get one value back. Is this the left or the right binEdge or in my case the lower or upper Edge? – Chaostante Jan 06 '17 at 12:25
  • 1
    Don't ask - try it yourself... :) (hint: the `BinEdges` vector is longer by 1 than `Values`) – Dev-iL Jan 06 '17 at 12:32

1 Answers1

1

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.

Dev-iL
  • 23,742
  • 7
  • 57
  • 99