Michaël
Michaël

Reputation: 71

Understanding Matlab histcounts behavior

histcounts(1:100,'BinWidth',50)

returns

49    51

Why doesn't it return

50    50

instead?

Upvotes: 1

Views: 969

Answers (1)

If_You_Say_So
If_You_Say_So

Reputation: 1283

Histogramming 1 to 100 inclusive with h = histogram(1:100, 'BinWidth', 50) gives:

enter image description here

Let's see the bin edges:

h.BinEdges

ans =

     0    50   100

From MATLAB's help:

Each bin includes the left edge, but does not include the right edge, except for the last bin which includes both edges

That means that values 1 to 100 are histogrammed in this format:

Bin 1 => edges: [0 50) => Included values: [1, 2, 3, .., 49]         (n = 49)
Bin 2 => edges: [50 100] => Included values: [50, 51, 52, .., 100]   (n = 51)

histcount(X) partitions X in the same manner as histogram(X). Therefore, the results are what you should expect and in fact very reasonable.

Upvotes: 1

Related Questions