1

I'm having some trouble in calculating the Mahalanobis Distance between a pair of objects. I followed the documentation of MATLAB where in order to calculate Mahalanobis Distance, I have to use pdist2: "D = pdist2(X,Y,'mahalanobis',C)"

A1=[75 87 90]; A2=[99 88 100];
C = nancov(A1,A2);
D = pdist2(A1,A2,'mahalanobis',C)

which gives me the error:

Error using pdist2 (line 282) The covariance matrix for the Mahalanobis metric must be a square matrix with the same number of columns as X. And it must be symmetric and positive definite.

on the other side if I try:

A1=[75 87 90]; A2=[99 88 100];
D = mahal(A1,A2)

I get:

Error using mahal (line 38) The number of rows of X must exceed the number of columns.

transposing A1,A2 I get a 3x1 matrix, but I'm pretty sure my value has to be one dimensional. Any help would be highly appreciated.

FL93
  • 29
  • 5
  • You can't calculate a Mahalanobis distance between just 2 vectors without more data to compute the covariance! This doesn't make sense. – buzjwa Jan 26 '17 at 08:01
  • Mahalanobis takes in two vectors and an *inverted* covariance array. – john k Nov 19 '17 at 19:44

1 Answers1

0

For a pair of objects, what could be done is to standardize first the X matrix for a 0-mean and 1-variance scale, and then apply Euclidean distance

Vfd1990
  • 11
  • 1