Function Reference: histcounts

datatypes: N = histcounts (X)
datatypes: N = histcounts (X, nbins)
datatypes: N = histcounts (X, edges)
datatypes: N = histcounts (…, Name, Value)
datatypes: [N, edges] = histcounts (…)
datatypes: [N, edges, bin] = histcounts (…)

Histogram bin counts.

N = histcounts (X) partitions the values of X into bins chosen automatically and returns the number of elements in each bin as a row vector. X is treated as X(:) regardless of its shape, and NaN and infinite values are excluded.

N = histcounts (X, nbins) uses nbins bins, and N = histcounts (X, edges) uses the bin edges given in the monotonically non-decreasing vector edges. Bin j covers [edges(j), edges(j+1)), except the last bin, which is closed at both ends.

[N, edges, bin] = histcounts (…) also returns the bin edges and, in bin, the index of the bin each element of X belongs to. bin has the same size as X and holds 0 for elements that fall in no bin. Note this differs from discretize, which returns NaN for such elements.

The following Name, Value pairs are supported. When more than one of 'NumBins', 'BinWidth', 'BinEdges' and 'BinMethod' is given, the last one specified takes effect.

NameValue
'NumBins'A positive integer scalar giving the number of bins, equivalent to the nbins syntax.
'BinEdges'A vector of bin edges, equivalent to the edges syntax.
'BinWidth'A positive finite scalar giving a uniform bin width. The edges are placed at multiples of the width covering the data.
'BinLimits'A two-element vector [lo, hi] restricting the binning to that range. Values outside it are not counted, and the outermost edges are clamped to lo and hi.
'BinMethod'One of 'auto' (default), 'scott', 'fd', 'integers', 'sturges' or 'sqrt'. 'auto' uses the 'integers' rule when the data are integer-valued and span at most 50, and 'scott' otherwise.
'Normalization'One of 'count' (default), 'countdensity', 'cumcount', 'probability', 'percentage', 'pdf' or 'cdf'. The divisor for 'probability', 'percentage', 'pdf' and 'cdf' is numel (X), not the number of elements actually counted, so values excluded by being NaN or out of range still contribute to it.

Source Code: histcounts

MATLAB accepts 'percentage' but omits it from the list of valid values in its own error message; it is documented here.

See also: discretize

Source Code: histcounts

Automatic binning. Integer-valued data spanning at most 50 gets one bin per integer, with edges at the half-integers.

 [N, edges] = histcounts ([1, 2, 3, 4, 5])
N =

   1   1   1   1   1

edges =

   0.5000   1.5000   2.5000   3.5000   4.5000   5.5000

Explicit edges, and the bin each value landed in.

 [N, edges, bin] = histcounts ([0, 1, 2, 3, 4, 5, 6], [1, 3, 5])
N =

   2   3

edges =

   1   3   5

bin =

   0   1   1   2   2   2   0

Normalization by relative frequency. The divisor is numel (X), so the NaN below still counts against the total.

 histcounts ([1, NaN, 2, 3], [1, 2, 3, 4], 'Normalization', 'probability')
ans =

   0.2500   0.2500   0.2500