# Mahalanobis distance

I have two vectors. I want to evaluate Mahalanobis distance between them. None of software packages are able to evaluate the distance producing a warning that the number of rows should be greater than than number of columns, so as to obtain inverse of covariance matrix. Can a covariance matrix be set up manually to evaluate Mahalanobis distance? For a simple example.

v1 = [172.14    193.43  155.99  106.92  127.37  142.18
110.03    56.92   36.27   0.48    2.53    3.05
2.18  0.17    0.10    1.61    80.70   89.70]
v2 = [170.61    192.41  156.66  106.50  125.50  142.43
110.18    57.02   35.94   0.42    2.54    2.94
2.16  0.12    0.16    1.61    81.50   88.20]

• Yes it can! the rest is software specific. Commented Jan 22, 2015 at 8:26
• Your example does not reflect situation where number of rows smaller than than number of columns.
– Tim
Commented Jan 22, 2015 at 8:42
• I'm not really sure of understanding what you need. Concerning your question:<br/> Can a covariance matrix be set up manually to evaluate Mahalanobis distance?<br/> I would say that your Mahalanobis distance is $\scriptsize\sum$ dependant, so you have to set your covariance matrix before concidering this distance. Can you clarify your question? Commented Jan 22, 2015 at 11:03

Mahalanobis distance is just

$$D(v_1, v_2) = \sqrt{ (v_1-v_2)^T \Sigma^{-1} (v_1-v_2) }$$

Where $\Sigma$ is a covariance matrix (it has to be strictly positive definite).

If you don't know this matrix (as often is the case), you can estimate it from data. But then you need to get as many examples as there are features (columns), otherwise the estimate will not be of full rank (and, thus, be non-invertable). Also, this is necessary condition, but not sufficient.

As long as your software allows you to invert matrices and multiply them, you can calculate this distance.

• Thanks for your reply! I know the formula. I cannot calculate Mahalanobis distance having vectors as rows, that v1 is 1x18 and and v2 is 1x18 using R or Matlab. However, I can calculate using these vectors as columns, but that's not what I am after. Commented Jan 22, 2015 at 9:00
• What's the difference between having vectors as columns and having them as rows? Commented Jan 22, 2015 at 9:03
• If I have them as rows, software packages cannot evaluate the distance, as the number of rows must exceed the number of columns. Commented Jan 22, 2015 at 9:21
• I don't know what kind of software you use (and what it does), but the Mahalanobis distance doesn't change because of transposition (moreover, vectors are 1-dimensional structures). If your software allows you to do linear algebra, you can specify $\Sigma$ manually, and then calculate the distance using the formula above. Commented Jan 22, 2015 at 9:30
• I've stated software I am using - R and Matlab. The problem has been discussed at least once on this site, with no solution provided. Most software packages are clear that in a matrix or data frame , then number of rows must be greater than the number of columns for procedures able to evaluate the distance. Now, I am thinking of finding unbiased estimator of the distance that does not require inverse of covariance matrix. Commented Jan 22, 2015 at 9:33