title: Mahalanobis distance in Matlab
body: <p>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. </p>
  
  <p>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.</p>
  
  <p>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. </p>
  
  <pre><code>Y = test1;         % Y: 1x14 vector 
  S = cov(X);        % X: 18x14 matrix 
  mu = mean(X,1);
  d = ((Y-mu)/S)*(Y-mu)' 
  </code></pre>
  
  <p>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!  </p>
  
  <pre><code> 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)
  </code></pre>
  
  <p>One last thing, when I used <code>mahal</code> function provided by Matlab I got this error:
  Warning: Matrix is close to singular or badly scaled. Results may be inaccurate.</p>