Function Reference: discretize

datatypes: bin = discretize (X, edges)
datatypes: bin = discretize (X, N)
datatypes: Y = discretize (…, values)
datatypes: C = discretize (…, ’categorical’)
datatypes: C = discretize (…, ’categorical’, names)
datatypes: Y = discretize (…, ’IncludedEdge’, side)
datatypes: [bin, edges] = discretize (…)

Group data into bins or categories.

bin = discretize (X, edges) returns an array of the same size as X whose elements give the index of the bin that each value of X falls into. edges must be a real numeric or logical vector of monotonically non-decreasing values, and defines numel (edges) - 1 bins. Bin j covers the half-open interval [edges(j), edges(j+1)), except the last bin, which is closed at both ends. Values outside [edges(1), edges(end)], and any NaN, return NaN. bin is always of type double.

Repeated edges are permitted and meaningful: they define empty bins, which are simply never selected. discretize ([1, 2, 3], [1, 2, 2, 3]) returns [1, 3, 3].

bin = discretize (X, N) uses N bins of uniform width spanning the range of X, where N is a positive integer scalar. The edges are placed at "nice" decimal positions rather than exactly at min (X) and max (X), so the bins generally extend slightly beyond the data. NaN and infinite values are ignored when determining the range. This syntax is not available when X is of an integer type; supply explicit edges instead.

Y = discretize (…, values) returns the corresponding element of values in place of the bin index, so values must be a vector whose length equals the number of bins. Y takes the type of values. Elements of X that fall in no bin return NaN when values is a floating-point array, zero when it is of an integer type, and raise an error when it is a cell array.

C = discretize (…, 'categorical') returns an ordinal categorical array whose categories are named after the bin intervals, for example '[1, 3)'. C = discretize (…, 'categorical', names) names the categories explicitly; names must be a cell array of character vectors or a string vector whose length equals the number of bins.

Y = discretize (…, 'IncludedEdge', side) selects which end of each bin is closed. side may be 'left' (the default, giving [edges(j), edges(j+1)) with the last bin closed at both ends) or 'right' (giving (edges(j), edges(j+1)] with the first bin closed at both ends).

[bin, edges] = discretize (…) also returns the bin edges used. When the edges were supplied they are returned unchanged; when a bin count was requested they are returned as a row vector.

See also: histcounts, categorical

Source Code: discretize

Bin data with explicit edges. Bins are closed on the left, except the last, which is closed at both ends.

 discretize ([1, 2, 3, 4, 5], [1, 3, 5])
ans =

   1   1   2   2   2

Ask for a bin count instead. The edges land on round numbers, so they generally extend a little beyond the data.

 [bin, edges] = discretize ([1, 2, 3, 4, 5], 3)
bin =

   1   1   2   3   3

edges =

   1.0000   2.4000   3.8000   5.2000

Return a label per bin rather than an index. Every value must fall in a bin when the labels are a cell array.

 discretize ([1, 2, 3, 4, 5], [1, 3, 5], {'low', 'high'})
ans =
  1x5 cell array

    {'low'}    {'low'}    {'high'}    {'high'}    {'high'}