IRootLab
An Open-Source MATLAB toolbox for vibrational biospectroscopy
maha.m
Go to the documentation of this file.
1 %>@ingroup maths
2 %>@file
3 %> @brief Calculates the Mahalanobis distances of the rows in X
4 %
5 %> @param X
6 %> @return Vertical vector of distances
7 function distances = maha(X)
8 
9 if rank(cov(X)) < size(X, 2)
10  irerror('Covariance matrix is singular, try removing low-variance feature');
11 end;
12 
13 no = size(X, 1);
14 means = mean(X, 1);
15 Xcentered = X-repmat(means, no, 1);
16 distances = zeros(no, 1);
17 Cinv = inv(cov(Xcentered));
18 for i = 1:no
19  v = Xcentered(i, :)';
20  distances(i) = v'*Cinv*v;
21 end;
22 distances = sqrt(distances);
23 
function irerror(in s)
function maha(in X)