Main Content

mahal

Mahalanobis distance to class means of discriminant analysis classifier

Description

example

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

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 or CompactClassificationDiscriminant model object trained with fitcdiscr.

Predictor data, specified as numeric matrix of size n-by-p, where p is the number of predictors in mdl, and n is any positive integer. mahal computes the Mahalanobis distances from the rows of X to each of the K means of the classes in mdl.

Class labels, specified as n elements of mdl.Y, where n is the number of rows of X.

Output Arguments

collapse all

Squared Mahalanobis distance, returned as a numeric matrix or column vector. Size and meaning of M depends on whether you specify ClassLabels.

  • Without ClassLabels, 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 ClassLabels, 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 ClassLabels.

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