Calculate mahalanobis distance of matrix for each row

8 views (last 30 days)
I want to calculate mahalanobis distance of matrix A for each row with respect to average value of column.
I use 'mahal' function in matlab
A = [110 -100 0; 100 -100 0; 10 0 0; 0 10 0; 0 -10 20; 0 -10 10; 0 0 10;-10 -10 -10; -10 0 0];
mean_row = mean(A,1);
mahal(mean_row,A)
But it gives zero value.
The result should be:
MD = [2.0937; 1.9970; 0.9072; 0.9517; 2.2387; 1.3394; 1.0792; 2.2596;0.9072]

Accepted Answer

Chunru
Chunru on 29 Nov 2021
Edited: Chunru on 29 Nov 2021
A = [110 -100 0; 100 -100 0; 10 0 0; 0 10 0; 0 -10 20; 0 -10 10; 0 0 10;-10 -10 -10; -10 0 0];
mean_row = mean(A,1);
whos
Name Size Bytes Class Attributes A 9x3 216 double mean_row 1x3 24 double
d = pdist2(A, mean_row, 'mahalanobis')
d = 9×1
1.8757 1.7646 1.5110 1.6756 2.0319 1.0264 0.9549 2.2982 0.9496
  1 Comment
NA
NA on 30 Nov 2021
Edited: NA on 30 Nov 2021
I checked out other data set and found that the result is different from the code.
I have this data set:
A = [10 -10; 1 0; -1 0; 0 -1; 0 1 ;11 -10; -1 -1]
The Mahalanobis distance that I got from your code is
1.4725
1.2957
0.83996
0.74575
1.4513
1.5646
1.5287
but the actual result is
I do not understand why I could get same result.
I mean, same thing happens for this data set
A = [110 -100 0; 100 -100 0; 10 0 0; 0 10 0; 0 -10 20; 0 -10 10; 0 0 10;-10 -10 -10; -10 0 0];
the result should be
But I got different result.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!