Hind Fawzi
Hind Fawzi

Reputation: 55

Mahalanobis distance in Matlab

I would like to calculate the mahalanobis distance of input feature vector Y (1x14) to all feature vectors in matrix X (18x14). Each 6 vectors of X represent one class (So I have 3 classes). Then based on mahalanobis distances I will choose the vector that is the nearest to the input and classify it to one of the three classes as well.

My problem is when I use the following code I got only one value. How can I get mahalanobis distance between the input Y and every vector in X. So at the end I have 18 values and then I choose the smallest one. Any help will be appreciated. Thank you.

Note: I know that mahalanobis distance is a measure of the distance between a point P and a distribution D, but I don't how could this be applied in my situation.

Y = test1;         % Y: 1x14 vector 
S = cov(X);        % X: 18x14 matrix 
mu = mean(X,1);
d = ((Y-mu)/S)*(Y-mu)' 

I also tried to separate the matrix X into 3; so each one represent the feature vectors of one class. This is the code, but it doesn't work properly and I got 3 distances and some have negative value!

 Y = test1;
 X1 = Action1;
 S1 = cov(X1);
 mu1 = mean(X1,1);
 d1 = ((Y-mu1)/S1)*(Y-mu1)' 

 X2 = Action2;
 S2 = cov(X2);
 mu2 = mean(X2,1);
 d2 = ((Y-mu2)/S2)*(Y-mu2)'

 X3= Action3;
 S3 = cov(X3);
 mu3 = mean(X3,1);
 d3 = ((Y-mu3)/S3)*(Y-mu3)' 


d= [d1,d2,d3];
MahalanobisDist= min(d)

One last thing, when I used mahal function provided by Matlab I got this error: Warning: Matrix is close to singular or badly scaled. Results may be inaccurate.

Upvotes: 1

Views: 1193

Answers (1)

andrew
andrew

Reputation: 2469

If you have to implement the distance yourself (school assignment for instance) this is of absolutely no use to you, but if you just need to calculate the distance as an intermediate step for other calculations I highly recommend d = Pdist2(a,b, distance_measure) the documentation is on matlabs site

It computes the pairwise distance between a vector (or even a matrix) b and all elements in a and stores them in vector d where the columns correspond to entries in b and the rows are entries from a. So d(i,j) is the distance between row j in b and row i in a (hope that made sense). If you want it could even parameters to find the k nearest neighbors, it's a great function.

in your case you would use the following code and you'd end up with the distance between elements, and the index as well

%number of neighbors 
K = 1;

% X=18x14, Y=1x14, dist=18x1
[dist, iidx] = pdist2(X,Y,'mahalanobis','smallest',K); 

%to find the class, you can do something like this
num_samples_per_class = 6;
matching_class = ceil(iidx/ num_samples_per_class);

Upvotes: 1

Related Questions