]> Creatis software - CreaPhase.git/blob - octave_packages/nan-2.5.5/mahal.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / nan-2.5.5 / mahal.m
1 function [d] = mahal(X,Y)
2 % MAHAL return the Mahalanobis' D-square distance between the 
3 % multivariate samples x and y, which must have the same number 
4 % of components (columns), but may have a different number of observations (rows). 
5
6 %  d = mahal(X,Y)
7 %
8 %   d(k) = (X(k,:)-MU)*inv(SIGMA)*(X(k,:)-MU)'
9 %
10 %   where MU and SIGMA are the mean and the covariance matrix of Y 
11 %
12 %
13 % see also: TRAIN_SC, TEST_SC, COVM
14 %
15 % References: 
16
17 %       $Id: train_sc.m 2140 2009-07-02 12:03:55Z schloegl $
18 %       Copyright (C) 2009 by Alois Schloegl <alois.schloegl@gmail.com>
19 %       This function is part of the NaN-toolbox
20 %       http://pub.ist.ac.at/~schloegl/matlab/NaN/
21
22 % This program is free software; you can redistribute it and/or
23 % modify it under the terms of the GNU General Public License
24 % as published by the Free Software Foundation; either version 2
25 % of the  License, or (at your option) any later version.
26
27 % This program is distributed in the hope that it will be useful,
28 % but WITHOUT ANY WARRANTY; without even the implied warranty of
29 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30 % GNU General Public License for more details.
31
32 % You should have received a copy of the GNU General Public License
33 % along with this program; if not, write to the Free Software
34 % Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
35
36 sx = size(X);
37 sy = size(Y);
38
39 if sx(2)~=sy(2),
40         error('number of columns of X and Y do not fit');
41 end;    
42
43 IR = inv(covm(Y,'E'));
44 IR(1,1) = IR(1,1)-1;
45
46 D = [ones(size(X,1),1),X];
47 d = sum((D*IR).*D,2);
48