Main Content

mahal

Mahalanobis distance to class means of discriminant analysis classifier

Description

M = mahal(Mdl,Tbl) returns the squared Mahalanobis distances from observations (rows) in Tbl to the class means in Mdl.

M = mahal(Mdl,X) returns the squared Mahalanobis distances from observations (rows) in X to the class means in Mdl.

example

M = mahal(Mdl,Tbl,ClassLabels=labels) returns the squared Mahalanobis distance as a column vector M. Here, M(i) is the squared Mahalanobis distance from the ith row of Tbl to the mean for the class of the ith element of labels.

M = mahal(Mdl,X,ClassLabels=labels) returns the squared Mahalanobis distance as a column vector M. Here, M(i) is the squared Mahalanobis distance from the ith row of X to the mean for the class of the ith element of labels.

Examples

collapse all

Find the Mahalanobis distances from the mean of the Fisher iris data to the class means.

Create a classification model for the Fisher iris data.

load fisheriris
obj = fitcdiscr(meas,species,DiscrimType="quadratic");

Find the Mahalanobis distances using distinct covariance matrices for each class.

mahadist = mahal(obj,mean(meas))
mahadist = 1×3

  220.0667    5.0254   30.5804

Input Arguments

collapse all

Trained discriminant analysis classifier, specified as a ClassificationDiscriminant model object trained with fitcdiscr, or a CompactClassificationDiscriminant model object created with compact.

Sample data, specified as a table. Each row of Tbl corresponds to one observation, and each column corresponds to one predictor variable. Categorical predictor variables are not supported. Optionally, Tbl can contain an additional columns for the response variable, which can be categorical. Tbl must contain all of the predictors used to train the model. Multicolumn variables and cell arrays other than cell arrays of character vectors are not allowed.

If you trained Mdl using sample data contained in a table, then the input data for mahal must also be in a table.

Data Types: table

Predictor data, specified as a numeric matrix. Each row of X corresponds to one observation, and each column corresponds to one predictor variable. Categorical predictor variables are not supported. The variables in the columns of X must be the same as the variables used to train Mdl. The number of rows in X must equal the number of rows in Y.

If you trained Mdl using sample data contained in a matrix, then the input data for mahal must also be in a matrix.

Data Types: single | double

Class labels, specified as n elements of Mdl.ResponseName, where n is the number of rows of Tbl or X.

Example: m = mahal(Mdl,Tbl,ClassLabels=Tbl.(Mdl.ResponseName))

Example: m = mahal(Mdl,X,ClassLabels=Mdl.Y)

Output Arguments

collapse all

Squared Mahalanobis distance, returned as a numeric matrix or column vector. The size and meaning of M depend on whether you specify labels.

  • Without labels, M is a numeric matrix of size n-by-K, where K is the number of classes in Mdl, and n is the number of rows in X. M(i,j) is the squared Mahalanobis distance from the ith row of X to the mean of class j.

  • With labels, M is a column vector with n elements. M(i) is the squared Mahalanobis distance from the ith row of X to the mean for the class of the ith element of labels.

More About

collapse all

Mahalanobis Distance

The Mahalanobis distance d(x,y) between n-dimensional points x and y, with respect to a given n-by-n covariance matrix S, is

d(x,y)=(xy)TS1(xy).

Version History

Introduced in R2011b