3200
|
1 ## Copyright (C) 1996, 1997 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 |
7016
|
7 ## the Free Software Foundation; either version 3 of the License, or (at |
|
8 ## your option) any later version. |
3200
|
9 ## |
|
10 ## Octave is distributed in the hope that it will be useful, but |
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 ## General Public License for more details. |
|
14 ## |
|
15 ## You should have received a copy of the GNU General Public License |
7016
|
16 ## along with Octave; see the file COPYING. If not, see |
|
17 ## <http://www.gnu.org/licenses/>. |
3200
|
18 |
3367
|
19 ## -*- texinfo -*- |
|
20 ## @deftypefn {Function File} {} mahalanobis (@var{x}, @var{y}) |
|
21 ## Return the Mahalanobis' D-square distance between the multivariate |
|
22 ## samples @var{x} and @var{y}, which must have the same number of |
|
23 ## components (columns), but may have a different number of observations |
|
24 ## (rows). |
|
25 ## @end deftypefn |
3200
|
26 |
|
27 ## Author: Friedrich Leisch <leisch@ci.tuwien.ac.at> |
|
28 ## Created: July 1993 |
|
29 ## Adapted-By: jwe |
|
30 |
|
31 function retval = mahalanobis (X, Y) |
|
32 |
|
33 if (nargin != 2) |
6046
|
34 print_usage (); |
3200
|
35 endif |
|
36 |
|
37 [xr, xc] = size (X); |
|
38 [yr, yc] = size (Y); |
|
39 |
|
40 if (xc != yc) |
3458
|
41 error ("mahalanobis: X and Y must have the same number of columns"); |
3200
|
42 endif |
|
43 |
|
44 Xm = sum (X) / xr; |
|
45 Ym = sum (Y) / yr; |
|
46 |
|
47 X = X - ones (xr, 1) * Xm; |
|
48 Y = Y - ones (yr, 1) * Ym; |
|
49 |
|
50 W = (X' * X + Y' * Y) / (xr + yr - 2); |
|
51 |
|
52 Winv = inv (W); |
|
53 |
|
54 retval = (Xm - Ym) * Winv * (Xm - Ym)'; |
|
55 |
|
56 endfunction |