time | calls | line |
---|
| | 1 | function xy = cov(x,varargin)
|
| | 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), where X and Y are matrices with
|
| | 8 | % the same number of elements, is equivalent to COV([X(:) Y(:)]).
|
| | 9 | %
|
| | 10 | % COV(X) or COV(X,Y) normalizes by (N-1) if N>1, where N is the number of
|
| | 11 | % observations. This makes COV(X) the best unbiased estimate of the
|
| | 12 | % covariance matrix if the observations are from a normal distribution.
|
| | 13 | % For N=1, COV normalizes by N.
|
| | 14 | %
|
| | 15 | % COV(X,1) or COV(X,Y,1) normalizes by N and produces the second
|
| | 16 | % moment matrix of the observations about their mean. COV(X,Y,0) is
|
| | 17 | % the same as COV(X,Y) and COV(X,0) is the same as COV(X).
|
| | 18 | %
|
| | 19 | % The mean is removed from each column before calculating the
|
| | 20 | % result.
|
| | 21 | %
|
| | 22 | % Class support for inputs X,Y:
|
| | 23 | % float: double, single
|
| | 24 | %
|
| | 25 | % See also CORRCOEF, VAR, STD, MEAN.
|
| | 26 |
|
| | 27 | % Copyright 1984-2010 The MathWorks, Inc.
|
| | 28 |
|
| 61 | 29 | if nargin==0
|
| | 30 | error(message('MATLAB:cov:NotEnoughInputs'));
|
| | 31 | end
|
| 61 | 32 | if nargin>3
|
| | 33 | error(message('MATLAB:cov:TooManyInputs'));
|
| | 34 | end
|
| 61 | 35 | if ~ismatrix(x)
|
| | 36 | error(message('MATLAB:cov:InputDim'));
|
| | 37 | end
|
| | 38 |
|
| 61 | 39 | nin = nargin;
|
| | 40 |
|
| | 41 | % Check for cov(x,flag) or cov(x,y,flag)
|
| 61 | 42 | if nin==3
|
| | 43 | flag = varargin{end};
|
| | 44 | if ~iscovflag(flag)
|
| | 45 | error(message('MATLAB:cov:notScalarFlag'));
|
| | 46 | end
|
| | 47 | nin = nin - 1;
|
| 61 | 48 | elseif nin==2 && iscovflag(varargin{end})
|
| | 49 | flag = varargin{end};
|
| | 50 | nin = nin - 1;
|
| 61 | 51 | else
|
| 61 | 52 | flag = 0;
|
| 61 | 53 | end
|
| | 54 |
|
| 61 | 55 | scalarxy = false; % cov(scalar,scalar) is an ambiguous case
|
| 61 | 56 | if nin == 2
|
| | 57 | y = varargin{1};
|
| | 58 | if ~ismatrix(y)
|
| | 59 | error(message('MATLAB:cov:InputDim'));
|
| | 60 | end
|
| | 61 | x = x(:);
|
| | 62 | y = y(:);
|
| | 63 | if length(x) ~= length(y),
|
| | 64 | error(message('MATLAB:cov:XYlengthMismatch'));
|
| | 65 | end
|
| | 66 | scalarxy = isscalar(x) && isscalar(y);
|
| | 67 | x = [x y];
|
| | 68 | end
|
| | 69 |
|
| 61 | 70 | if isvector(x) && ~scalarxy
|
| | 71 | x = x(:);
|
| | 72 | end
|
| | 73 |
|
| 61 | 74 | [m,n] = size(x);
|
| 61 | 75 | if isempty(x);
|
| | 76 | if (m==0 && n==0)
|
| | 77 | xy = NaN(class(x));
|
| | 78 | else
|
| | 79 | xy = NaN(n,class(x));
|
| | 80 | end
|
| | 81 | return;
|
| | 82 | end
|
| | 83 |
|
| 61 | 84 | if m == 1 % One observation
|
| | 85 |
|
| | 86 | % For single data, unbiased estimate of the covariance matrix is not defined.
|
| | 87 | % Return the second moment matrix of the observations about their mean.
|
| | 88 | xy = zeros(n,class(x));
|
| | 89 |
|
| 61 | 90 | else
|
| | 91 |
|
0.01 | 61 | 92 | xc = bsxfun(@minus,x,sum(x,1)/m); % Remove mean
|
| 61 | 93 | if flag
|
| | 94 | xy = (xc' * xc) / m;
|
| 61 | 95 | else
|
0.03 | 61 | 96 | xy = (xc' * xc) / (m-1);
|
| 61 | 97 | end
|
| | 98 |
|
| 61 | 99 | end
|