1
votes

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.

1
You can't calculate a Mahalanobis distance between just 2 vectors without more data to compute the covariance! This doesn't make sense.buzjwa
Mahalanobis takes in two vectors and an inverted covariance array.john ktejik

1 Answers

0
votes

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