7017
|
1 ## Copyright (C) 1996, 1997, 1998, 1999, 2004, 2005, 2006, 2007 |
|
2 ## John W. Eaton |
3200
|
3 ## |
|
4 ## This file is part of Octave. |
|
5 ## |
|
6 ## Octave is free software; you can redistribute it and/or modify it |
|
7 ## under the terms of the GNU General Public License as published by |
7016
|
8 ## the Free Software Foundation; either version 3 of the License, or (at |
|
9 ## your option) any later version. |
3200
|
10 ## |
|
11 ## Octave is distributed in the hope that it will be useful, but |
|
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
14 ## General Public License for more details. |
|
15 ## |
|
16 ## You should have received a copy of the GNU General Public License |
7016
|
17 ## along with Octave; see the file COPYING. If not, see |
|
18 ## <http://www.gnu.org/licenses/>. |
3200
|
19 |
3367
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {} corrcoef (@var{x}, @var{y}) |
6754
|
22 ## Compute correlation. |
|
23 ## |
3367
|
24 ## If each row of @var{x} and @var{y} is an observation and each column is |
4855
|
25 ## a variable, the (@var{i}, @var{j})-th entry of |
3367
|
26 ## @code{corrcoef (@var{x}, @var{y})} is the correlation between the |
|
27 ## @var{i}-th variable in @var{x} and the @var{j}-th variable in @var{y}. |
6754
|
28 ## |
|
29 ## @iftex |
|
30 ## @tex |
|
31 ## $$ |
|
32 ## {\rm corrcoef}(x,y) = {{\rm cov}(x,y) \over {\rm std}(x) {\rm std}(y)} |
|
33 ## $$ |
|
34 ## @end tex |
|
35 ## @end iftex |
|
36 ## @ifnottex |
|
37 ## @example |
|
38 ## corrcoef(x,y) = cov(x,y)/(std(x)*std(y)) |
|
39 ## @end example |
|
40 ## @end ifnottex |
|
41 ## |
3367
|
42 ## If called with one argument, compute @code{corrcoef (@var{x}, @var{x})}. |
|
43 ## @end deftypefn |
3200
|
44 |
5428
|
45 ## Author: Kurt Hornik <hornik@wu-wien.ac.at> |
3200
|
46 ## Created: March 1993 |
|
47 ## Adapted-By: jwe |
|
48 |
|
49 function retval = corrcoef (x, y) |
|
50 |
|
51 if (nargin < 1 || nargin > 2) |
6046
|
52 print_usage (); |
3200
|
53 endif |
|
54 |
|
55 if (nargin == 2) |
|
56 c = cov (x, y); |
|
57 s = std (x)' * std (y); |
|
58 retval = c ./ s; |
|
59 elseif (nargin == 1) |
|
60 c = cov (x); |
|
61 s = reshape (sqrt (diag (c)), 1, columns (c)); |
|
62 retval = c ./ (s' * s); |
|
63 endif |
|
64 |
|
65 endfunction |