# Documentation

### This is machine translation

Translated by
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

# mahal

Mahalanobis distance to class means

## Syntax

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

## Description

`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

 `obj` Discriminant analysis classifier of class `ClassificationDiscriminant` or `CompactClassificationDiscriminant`, typically constructed with `fitcdiscr`. `X` 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 Pair 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 single quotes (`' '`). You can specify several name and value pair arguments in any order as `Name1,Value1,...,NameN,ValueN`.

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

## Output Arguments

 `M` Size and meaning of output `M` depends on whether the `ClassLabels` name-value pair is present: No `ClassLabels` — `M` 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 `i`th 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 `i`th row of `X` to the mean for the class of the `i`th element of `ClassLabels`.

## Definitions

### 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\left(x,y\right)=\sqrt{{\left(x-y\right)}^{T}{S}^{-1}\left(x-y\right)}.$`

## Examples

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,... 'DiscrimType','quadratic'); mahadist = mahal(obj,mean(meas)) mahadist = 220.0667 5.0254 30.5804```

## How To

Was this topic helpful?

Download now