mahaldist.m
%mahaldist: Mahalanobis distance between X(i, :) and Y(j, :)
%
% [d] = mahaldist(X, Y)
%
% Input and output arguments ([]'s are optional):
% X (matrix) of size NxD. N is the number of vectors,
% and D is the dimension of each vector.
% Y (matrix) of size PxD. P is the number of vectors,
% and D is the dimension of each vector.
% d (matrix) of size NxP. d(i, j) is the mahalanobis distance
% between X(i, :) and Y(j, :).
%
% Reference: pdist of statistics toolbox
%
% Author : Naotoshi Seo
% Date : June, 2006
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function d = mahaldist(X, Y)
[N D] = size(X);
[P D] = size(Y);
A = [X; Y]; % pdist was doing as 2nd way, but 1st way was faster.
invcov = inv(cov(A));
% invcov = cov(A) / eye(D); for i=1:N
% Compute distances from one vector toward vectors at burst.
diff = repmat(X(i, :), P, 1) - Y;
%dsq(i, :) = diag(diff * invcov * diff'); % only diag is necessary, burdernsome
dsq(i, :) = sum((diff*invcov).*diff , 2);
end
d = sqrt(dsq);
mahaldist_stats.m
%mahaldist_stats: Mahalanobis distance between X(i, :) and Y(j, :) using
% statistics toolbox
%
% [d] = mahaldist_stats(X, Y)
%
% Input and output arguments ([]'s are optional):
% X (matrix) of size NxD. N is the number of vectors,
% and D is the dimension of each vector.
% Y (matrix) of size PxD. P is the number of vectors,
% and D is the dimension of each vector.
% d (matrix) of size NxP. d(i, j) is the mahalanobis distance
% between X(i, :) and Y(j, :).
%
% Requirement: statistics toolbox
%
% Author : Naotoshi Seo
% Date : June, 2006
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function d = mahaldist_stats(X, Y)
[N D] = size(X);
[P D] = size(Y);
A = [X; Y];
% calculation of distance between X(i, :) and X(j, :) is wortheless, though
d = squareform(pdist(A, 'mahalanobis'));
d = d(1:N, N+1:end);
mahaldistTest.m
function mahaldistTest
proto = [
0.6213 0.7373
0.5226 0.8939
0.9797 0.6614
0.9568 0.0118
0.8801 0.1991
0.8757 0.0648
0.1730 0.2987
0.2714 0.2844
0.2523 0.4692
];
data = [
0.9883 0.4329
0.5828 0.2259
0.4235 0.5798
0.5155 0.7604
0.3340 0.5298
];
d = mahaldist(proto, data)
d_stats = mahaldist_stats(proto, data) X = rand(200, 100);
Y = rand(200, 100);
tic
mahaldist(X, Y);
toc
tic
mahaldist_stats(X, Y);
toc
d =
1.5219 1.9870 1.0235 0.3631 1.4269
2.1044 2.5058 1.3251 0.5157 1.6762
0.8648 2.4137 2.0606 1.5932 2.4486
1.6359 1.3790 2.5120 2.9112 2.5857
1.0500 1.0308 1.8977 2.2219 2.0397
1.5470 1.0710 2.2191 2.6570 2.2805
3.0419 1.4079 1.5431 2.3606 1.1565
2.7225 1.0694 1.3580 2.1863 1.0113
2.5706 1.3082 0.8151 1.6082 0.4102
d_stats =
1.5219 1.9870 1.0235 0.3631 1.4269
2.1044 2.5058 1.3251 0.5157 1.6762
0.8648 2.4137 2.0606 1.5932 2.4486
1.6359 1.3790 2.5120 2.9112 2.5857
1.0500 1.0308 1.8977 2.2219 2.0397
1.5470 1.0710 2.2191 2.6570 2.2805
3.0419 1.4079 1.5431 2.3606 1.1565
2.7225 1.0694 1.3580 2.1863 1.0113
2.5706 1.3082 0.8151 1.6082 0.4102