home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l460 / 2.ddi / DATAFUN.DI$ / COV.M < prev    next >
Encoding:
Text File  |  1993-03-07  |  811 b   |  32 lines

  1. function xy = cov(x,y)
  2. %COV    Covariance matrix.
  3. %    COV(X), if X is a vector, returns the variance.  For matrices,
  4. %    where each row is an observation, and each column a variable,
  5. %    COV(X) is the covariance matrix.  DIAG(COV(X)) is a vector of
  6. %    variances for each column, and SQRT(DIAG(COV(X))) is a vector
  7. %    of standard deviations.   COV(X,Y) is COV([X Y]).  
  8. %
  9. %    See also CORRCOEF, STD.
  10.  
  11. %    J. Little 5-5-86
  12. %    Revised 6-9-88 LS
  13. %    Copyright (c) 1984-93 by The MathWorks, Inc.
  14.  
  15. [m,n] = size(x);
  16. if nargin > 1
  17.     [my,ny] = size(y);
  18.     if m ~= my | n ~= ny
  19.         error('X and Y must be the same size.');
  20.     end
  21.     x = [x(:) y(:)];
  22. elseif min(size(x)) == 1
  23.     x = x(:);
  24. end
  25. [m, n] = size(x);
  26. x = x - ones(m,1) * (sum(x)/m);
  27. if m == 1
  28.     xy = 0;
  29. else
  30.     xy = x' * x / (m-1);
  31. end
  32.