comparison scripts/statistics/mahalanobis.m @ 283:f3ce570869fc

[project @ 1994-01-10 20:29:54 by jwe] Initial revision
author jwe
date Mon, 10 Jan 1994 20:30:01 +0000
parents
children 3c23b8ea9099
comparison
equal deleted inserted replaced
282:ea306e13ce22 283:f3ce570869fc
1 # Copyright (C) 1993 John W. Eaton
2 #
3 # This file is part of Octave.
4 #
5 # Octave is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by the
7 # Free Software Foundation; either version 2, or (at your option) any
8 # later version.
9 #
10 # Octave is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 # for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Octave; see the file COPYING. If not, write to the Free
17 # Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 function retval = mahalanobis (X, Y)
20
21 # usage: mahalanobis (X, Y)
22 #
23 # Returns Mahalanobis' D-square distance between the multivariate
24 # samples X and Y, which must have the same number of components
25 # (columns), but may have a different number of observations (rows).
26
27 # Written by Friedrich Leisch (leisch@neuro.tuwien.ac.at) July 1993.
28 # Dept of Probability Theory and Statistics TU Wien, Austria.
29
30 if (nargin != 2)
31 error ("usage: mahalanobis (X, Y)");
32 endif
33
34 [xr, xc] = size (X);
35 [yr, yc] = size (Y);
36
37 if (xc != yc)
38 error ("mahalanobis: X and Y must have the same number of columns.");
39 endif
40
41 Xm = sum (X) / xr;
42 Ym = sum (Y) / yr;
43
44 X = X - ones (xr, 1) * Xm;
45 Y = Y - ones (yr, 1) * Ym;
46
47 W = (X' * X + Y' * Y) / (xr + yr - 2);
48
49 Winv = inv (W);
50
51 retval = (Xm - Ym) * Winv * (Xm - Ym)';
52
53 endfunction