histc - (Not recommended) Histogram bin counts - MATLAB (original) (raw)

(Not recommended) Histogram bin counts

Syntax

Description

[bincounts](#btx9b%5Fu-1-bincounts) = histc([x](#btx9b%5Fu-1-x),[binranges](#btx9b%5Fu-1-binranges)) counts the number of values in x that are within each specified bin range. The input, binranges, determines the endpoints for each bin. The output,bincounts, contains the number of elements from x in each bin.

To plot the histogram, use bar(binranges,bincounts,'histc').

example

[bincounts](#btx9b%5Fu-1-bincounts) = histc([x](#btx9b%5Fu-1-x),[binranges](#btx9b%5Fu-1-binranges),[dim](#btx9b%5Fu-1-dim)) operates along the dimension dim.

[[bincounts](#btx9b%5Fu-1-bincounts),[ind](#btx9b%5Fu-1-ind)] = histc(___) returns ind, an array the same size asx indicating the bin number that each entry in x sorts into. Use this syntax with any of the previous input argument combinations.

example

Examples

collapse all

Initialize the random number generator to make the output of randn repeatable.

Define 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 bincounts.

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 bar function.

figure bar(binranges,bincounts,'histc')

Figure contains an axes object. The axes object contains an object of type patch.

Defined 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.

Input Arguments

collapse all

Values to be sorted, specified as a vector or a matrix. The bin counts do not include values in x that are NaN or that lie outside the specified bin ranges. If x contains complex values, thenhistc ignores the imaginary parts and uses only the real parts.

Data Types: single | double | int8 | int16 | int32 | uint8 | uint16 | uint32

Bin ranges, specified as a vector of monotonically nondecreasing values or a matrix of monotonically nondecreasing values running down each successive column. The values inbinranges determine the left and right endpoints for each bin. Ifbinranges contains complex values, then histc ignores the imaginary parts and uses only the real parts.

If binranges is a matrix, then histc determines 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 binranges.

For example, if binranges equals the vector[0,5,10,13], then 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.

Data Types: single | double | int8 | int16 | int32 | uint8 | uint16 | uint32

Dimension along which to operate, specified as a scalar.

Output Arguments

collapse all

Number of elements in each bin, returned as a vector or a matrix. The last entry inbincounts is the number of values in x that equal the last entry in binranges.

Bin index numbers, returned as a vector or a matrix that is the same size asx.

Tips

Extended Capabilities

expand all

Usage notes and limitations:

Version History

Introduced before R2006a