histcounts

Counts how many values fall into each bin.

Category: Data Import and Analysis > Preprocessing Data

Syntax

[N, edges] = histcounts(X)

Bins X with automatically chosen, equal-width bins.

[N, edges] = histcounts(X, nbins)

Uses nbins equal-width bins.

[N, edges] = histcounts(X, edges)

Uses the given bin edges.

N = histcounts(___, 'Normalization', s)

Scales the counts, for example 'probability'.

N = histcounts(___, 'BinWidth', w)

Uses bins of width w.

Description

Each bin includes its left edge and excludes its right edge, except the last bin, which includes both. Values outside the range of the edges, and NaN, are not counted. The function returns numbers only; histogram draws the same data.

With 'probability' the counts are divided by the total number of elements, so they sum to 1 when all data fall inside the bins.

Input arguments

X
Numeric array; the elements are counted regardless of shape.
nbins
Number of bins.
edges
Increasing vector of bin edges.
'Normalization'
'count' (default), 'probability', 'countdensity', 'pdf', 'cumcount' or 'cdf'.

Output arguments

N
Row vector with one value per bin.
edges
Bin edges; N has one fewer element.

Example

Explicit edges

x = [1 2 2 3 5 8];
N = histcounts(x, [0 2 4 10])
histcounts(x, [0 2 4 10], 'Normalization', 'probability')
[N, e] = histcounts(x)
Output
N = 1×3

     1     3     2
ans = 1×3

    0.1667    0.5000    0.3333
N = 1×8

     1     2     1     0     1     0     0     1
e = 1×9

    0.5000    1.5000    2.5000    3.5000    4.5000    5.5000    6.5000    7.5000    8.5000

See also

histcounts2 histogram accumarray discretize