dummyvar
Create dummy variables
Syntax
Description
returns a matrix D
= dummyvar(group
)D
containing zeros and ones, whose columns are
dummy variables for the grouping variables in
group
. Each column of group
is a
single grouping variable, with values indicating category levels. The rows of
group
represent observations across all variables.
Examples
Input Arguments
Output Arguments
Tips
Use dummy variables in regression analysis and ANOVA to indicate values of categorical predictors.
dummyvar
treatsNaN
values and undefined categorical levels ingroup
as missing data and returnsNaN
values inD
.If a column of ones is introduced in the matrix
D
, then the resulting matrixX = [ones(size(D,1),1) D]
is rank deficient. Ifgroup
has multiple columns, then the matrixD
itself is rank deficient because dummy variables produced from any column ofgroup
always sum to a column of ones. Regression and ANOVA calculations often address this issue by eliminating one dummy variable (implicitly setting the coefficients for dropped columns to zero) from each group of dummy variables produced by a column ofgroup
.If
group
is a numeric vector with levels that do not correspond exactly to the integers1:max(group)
, first convert the data to a categorical vector by usingcategorical
. You can then pass the result todummyvar
. For an example, see Create Dummy Variables from Multiple Grouping Variables.
Alternative Functionality
Alternatively, use onehotencode
to encode data labels. Consider using
onehotencode
instead of dummyvar
in these
cases:
To encode a table of categorical data labels
To specify the dimension to expand for encoding the data labels
Extended Capabilities
Version History
Introduced before R2006a