Mercurial > hg > octave-max
annotate scripts/statistics/base/cor.m @ 11213:009d16b010fa
lo-mappers.cc (xmod, xrem): don't copy sign if result is zero
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Tue, 09 Nov 2010 13:21:15 -0500 |
parents | 693e22af08ae |
children | e151e23f73bc |
rev | line source |
---|---|
7017 | 1 ## Copyright (C) 1995, 1996, 1997, 1998, 2000, 2002, 2004, 2005, 2006, |
9245 | 2 ## 2007, 2009 Kurt Hornik |
3426 | 3 ## |
3922 | 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. | |
3426 | 10 ## |
3922 | 11 ## Octave is distributed in the hope that it will be useful, but |
3200 | 12 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
3426 | 14 ## General Public License for more details. |
15 ## | |
3200 | 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 |
3453 | 20 ## -*- texinfo -*- |
21 ## @deftypefn {Function File} {} cor (@var{x}, @var{y}) | |
6754 | 22 ## Compute correlation. |
23 ## | |
4855 | 24 ## The (@var{i}, @var{j})-th entry of @code{cor (@var{x}, @var{y})} is |
3453 | 25 ## the correlation between the @var{i}-th variable in @var{x} and the |
26 ## @var{j}-th variable in @var{y}. | |
3200 | 27 ## |
6754 | 28 ## @tex |
29 ## $$ | |
30 ## {\rm corrcoef}(x,y) = {{\rm cov}(x,y) \over {\rm std}(x) {\rm std}(y)} | |
31 ## $$ | |
32 ## @end tex | |
33 ## @ifnottex | |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
34 ## |
6754 | 35 ## @example |
36 ## corrcoef(x,y) = cov(x,y)/(std(x)*std(y)) | |
37 ## @end example | |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
38 ## |
6754 | 39 ## @end ifnottex |
3200 | 40 ## For matrices, each row is an observation and each column a variable; |
41 ## vectors are always observations and may be row or column vectors. | |
42 ## | |
3453 | 43 ## @code{cor (@var{x})} is equivalent to @code{cor (@var{x}, @var{x})}. |
6754 | 44 ## |
45 ## Note that the @code{corrcoef} function does the same as @code{cor}. | |
3453 | 46 ## @end deftypefn |
3200 | 47 |
5428 | 48 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
3456 | 49 ## Description: Compute correlations |
3200 | 50 |
51 function retval = cor (x, y) | |
52 | |
53 if (nargin < 1 || nargin > 2) | |
6046 | 54 print_usage (); |
3200 | 55 endif |
56 | |
57 if (nargin == 2) | |
58 c = cov (x, y); | |
59 s = std (x)' * std (y); | |
60 retval = c ./ s; | |
61 elseif (nargin == 1) | |
62 c = cov (x); | |
63 s = reshape (sqrt (diag (c)), 1, columns (c)); | |
64 retval = c ./ (s' * s); | |
65 endif | |
66 | |
67 endfunction |