-
Notifications
You must be signed in to change notification settings - Fork 10
/
cvMahaldist.m
61 lines (60 loc) · 1.8 KB
/
cvMahaldist.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
function d = cvMahaldist(X, Y)
% cvMahaldist - Mahalanobis distance
%
% Synopsis
% [d] = cvMahaldist(X, Y)
%
% Description
% Compute mahalanobis distance between X and Y.
%
% Inputs ([]s are optional)
% (matrix) X D x N matrix where D is the dimension of vectors
% and N is the number of vectors.
% (matrix) Y D x P matrix where D is the dimension of vectors
% and P is the number of vectors.
%
% Outputs ([]s are optional)
% (matrix) d N x P matrix where d(n,p) represents the mahalanobis
% distance between X(:,n) and Y(:,p).
%
% Examples
% X = [1 2
% 1 2];
% Y = [1 2 3
% 1 2 3];
% d = cvMahaldist(X, Y)
%
% See also
% cvEucdist, pdist (statistics Toolbox), mahal (statistics toolbox)
% References
% [1] http://en.wikipedia.org/wiki/Mahalanobis_distance
%
% Authors
% Naotoshi Seo <sonots(at)sonots.com>
%
% License
% The program is free to use for non-commercial academic purposes,
% but for course works, you must understand what is going inside to use.
% The program can be used, modified, or re-distributed for any purposes
% if you or one of your group understand codes (the one must come to
% court if court cases occur.) Please contact the authors if you are
% interested in using the program without meeting the above conditions.
%
% Changes
% 06/2006 First Edition
[D N] = size(X);
[D P] = size(Y);
A = [X Y];
invcov = inv(cov(A'));
for i=1:N
diff = repmat(X(:,i), 1, P) - Y;
dsq(i,:) = sum((invcov*diff).*diff , 1);
end
d = sqrt(dsq);
%% Use pdist.m (Statistics Toolbox). This becomes slow.
% [D N] = size(X);
% [D P] = 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);