histcounts2

Counts how many pairs of values fall into each two-dimensional bin.

Category: Data Import and Analysis > Preprocessing Data

Syntax

[N, xedges, yedges] = histcounts2(X, Y)

Bins the pairs (X, Y) with automatic bins.

N = histcounts2(X, Y, nbins)

Uses nbins bins in each direction.

N = histcounts2(X, Y, xedges, yedges)

Uses the given edges for each axis.

Description

histcounts2 is the two-dimensional version of histcounts. The first dimension of the result follows X and the second follows Y. The same left-closed, right-open rule applies to each axis, and points outside the edges are ignored.

The counts are useful for joint distributions, density maps and checking how two variables vary together.

Input arguments

X, Y
Numeric arrays of the same size; element k of each forms the point k.
xedges, yedges
Increasing vectors of bin edges.

Output arguments

N
Matrix of counts; N(i,j) counts points with X in bin i and Y in bin j.
xedges, yedges
The edges used along each axis.

Example

Counting points in cells

x = [1 2 3 3];
y = [1 2 3 1];
N = histcounts2(x, y, [0 2 4], [0 2 4])
Output
N = 2×2

     1     0
     1     2

See also

histcounts histogram2 accumarray discretize