3200
|
1 ## Copyright (C) 1995, 1996, 1997 Kurt Hornik |
3426
|
2 ## |
3922
|
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 |
3200
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## any later version. |
3426
|
9 ## |
3922
|
10 ## Octave is distributed in the hope that it will be useful, but |
3200
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
3426
|
13 ## General Public License for more details. |
|
14 ## |
3200
|
15 ## You should have received a copy of the GNU General Public License |
3922
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
|
17 ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA |
|
18 ## 02111-1307, USA. |
3200
|
19 |
3453
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {} cor (@var{x}, @var{y}) |
|
22 ## The (@var{i},@var{j})-th entry of @code{cor (@var{x}, @var{y})} is |
|
23 ## the correlation between the @var{i}-th variable in @var{x} and the |
|
24 ## @var{j}-th variable in @var{y}. |
3200
|
25 ## |
|
26 ## For matrices, each row is an observation and each column a variable; |
|
27 ## vectors are always observations and may be row or column vectors. |
|
28 ## |
3453
|
29 ## @code{cor (@var{x})} is equivalent to @code{cor (@var{x}, @var{x})}. |
|
30 ## @end deftypefn |
3200
|
31 |
3456
|
32 ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at> |
|
33 ## Description: Compute correlations |
3200
|
34 |
|
35 function retval = cor (x, y) |
|
36 |
|
37 if (nargin < 1 || nargin > 2) |
3456
|
38 usage ("cor (x, y)"); |
3200
|
39 endif |
|
40 |
|
41 if (nargin == 2) |
|
42 c = cov (x, y); |
|
43 s = std (x)' * std (y); |
|
44 retval = c ./ s; |
|
45 elseif (nargin == 1) |
|
46 c = cov (x); |
|
47 s = reshape (sqrt (diag (c)), 1, columns (c)); |
|
48 retval = c ./ (s' * s); |
|
49 endif |
|
50 |
|
51 endfunction |