Main Content


Mahalanobis distance to class means of discriminant analysis classifier


M = mahal(obj,X)
M = mahal(obj,X,Name,Value)


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

M = mahal(obj,X,Name,Value) computes the squared Mahalanobis distance with additional options specified by one or more Name,Value pair arguments.

Input Arguments


Discriminant analysis classifier of class ClassificationDiscriminant or CompactClassificationDiscriminant, typically constructed with fitcdiscr.


Numeric matrix of size n-by-p, where p is the number of predictors in obj, 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 obj.

Name-Value Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside quotes. You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.


Class labels consisting of n elements of obj.Y, where n is the number of rows of X.

Output Arguments


Size and meaning of output M depends on whether the ClassLabels name-value pair is present:

  • No ClassLabelsM is a numeric matrix of size n-by-K, where K is the number of classes in obj, 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.

  • ClassLabels exists — 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.


Find the Mahalanobis distances from the mean of the Fisher iris data to the class means, using distinct covariance matrices for each class:

load fisheriris
obj = fitcdiscr(meas,species,...
mahadist = mahal(obj,mean(meas))

mahadist =
  220.0667    5.0254   30.5804

More About

expand all