Histogram bin counts (not recommended; use
histc is not recommended. Use
For more information, including suggestions on updating code, see Replace Discouraged Instances of hist and histc.
counts the number of values in
bincounts = histc(
x that are within each specified bin range.
binranges, determines the endpoints for each bin. The output,
bincounts, contains the number of elements from
x in each
x is a vector, then
bincounts as a vector of histogram bin counts.
x is a matrix, then
histc operates along each
x and returns
bincounts as a matrix of
histogram bin counts for each column.
To plot the histogram, use
Initialize the random number generator to make the output of
x as 100 normally distributed random numbers. Define bin ranges between -4 and 4. Determine the number of values in
x that are within each specified bin range. Return the number of elements in each bin in
x = randn(100,1); binranges = -4:4; [bincounts] = histc(x,binranges)
bincounts = 9×1 0 2 17 28 32 16 3 2 0
To plot the histogram, use the
ages as a vector of ages. Sort
ages into bins with varying ranges between 0 and 75.
ages = [3,12,24,15,5,74,23,54,31,23,64,75]; binranges = [0,10,25,50,75]; [bincounts,ind] = histc(ages,binranges)
bincounts = 1×5 2 5 1 3 1
ind = 1×12 1 2 2 2 1 4 2 4 3 2 4 5
bincounts contains the number of values in each bin.
ind indicates the bin numbers.
x— Values to be sorted
Values to be sorted, specified as a vector or a matrix. The bin counts do not include
x that are
NaN or that lie outside the
specified bin ranges. If
x contains complex values, then
histc ignores the imaginary parts and uses only the real parts.
binranges— Bin ranges
Bin ranges, specified as a vector of monotonically nondecreasing values or a matrix of
monotonically nondecreasing values running down each successive column. The values in
binranges determine the left and right endpoints for each bin. If
binranges contains complex values, then
the imaginary parts and uses only the real parts.
binranges is a matrix, then
the bin ranges by using values running down successive columns. Each bin includes the left
endpoint, but does not include the right endpoint. The last bin consists of the scalar value
equal to last value in
For example, if
binranges equals the vector
histc creates four bins. The first
bin includes values greater than or equal to 0 and strictly less than 5. The second bin
includes values greater than or equal to 5 and less than 10, and so on. The last bin contains
the scalar value 13.
dim— Dimension along which to operate
Dimension along which to operate, specified as a scalar.
bincounts— Number of elements in each bin
Number of elements in each bin, returned as a vector or a matrix. The last entry in
bincounts is the number of values in
x that equal the
last entry in
ind— Bin index numbers
Bin index numbers, returned as a vector or a matrix that is the same size as
If values in
x lie outside the specified bin ranges, then
histc does not include these values in the bin counts. Start and end the
binranges vector with
ensure that all values in
x are included in the bin counts.
Usage notes and limitations:
The output of a variable-size array that becomes a column vector at run time is a column-vector, not a row-vector.
dim must be a constant.
See Variable-Sizing Restrictions for Code Generation of Toolbox Functions (MATLAB Coder).
backgroundPoolor accelerate code with Parallel Computing Toolbox™
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
This function fully supports distributed arrays. For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).