hist

Counts data into equally spaced bins and draws a histogram, or returns the counts and centers.

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

Syntax

hist(y)

Plots a histogram with 10 bins spanning the range of y.

hist(y, nbins)

Uses nbins bins.

hist(y, centers)

Uses the vector centers as the bin centers.

[n, c] = hist(___)

Returns the counts n and the bin centers c and draws nothing.

Description

With a number of bins, the range from the minimum to the maximum of the data is divided into equal parts, and the center of each part is reported. Each value is counted in the bin whose center is nearest.

hist is an older function. histogram offers automatic bin selection, normalization and a returned object, and histcounts returns counts without plotting.

Input arguments

y
Vector of data. For a matrix, each column is binned separately.
nbins
Number of bins, 10 by default.
centers
Vector of bin centers; the bin edges lie halfway between neighbors.

Output arguments

n
Counts per bin, as a row vector (one column per data column for a matrix input, with bins down the rows).
c
Centers of the bins.

Example

Counts and centers

y = [1 2 2 3 3 3 4 4 5 9];
[n, c] = hist(y, 3)
hist(y, [2 5 8])
Output
n = 1×3

     6     3     1
c = 1×3

    2.3333    5.0000    7.6667

See also

histogram histcounts histc bar accumarray