histc

Counts how many values fall in each interval defined by a vector of bin edges.

Category: Graphics > 2-D and 3-D Plots

Syntax

n = histc(x, edges)

Counts the elements of x in each bin edges(k) <= x < edges(k+1).

[n, bin] = histc(x, edges)

Also returns the bin index of each element of x.

n = histc(x, edges, dim)

Counts along dimension dim.

Description

Every bin is closed on the left and open on the right, except for the last, which holds only values exactly equal to the final edge. Values below the first edge or above the last one are not counted. For a matrix, each column is counted separately.

histc is kept for compatibility; histcounts is the recommended replacement and its last bin includes the right edge.

Input arguments

x
Vector or array of values.
edges
Monotonically non-decreasing vector of bin edges.
dim
Dimension along which to count; by default the first non-singleton one.

Output arguments

n
Counts, with as many elements along the counting dimension as edges. The last element counts values equal to edges(end).
bin
Bin index for each element of x, or 0 when it falls outside all bins.

Example

Counts and bin indices

x = [1 2 2 3 3 3 4 4 5 9];
[n, bin] = histc(x, [0 3 6 9])
Output
n = 1×4

     3     6     0     1
bin = 1×10

     1     1     1     2     2     2     2     2     2     4

See also

histcounts discretize hist accumarray histogram