This is a static copy of a profile reportHome
var (1673 calls, 2.186 sec)
Generated 05-Aug-2011 13:03:47 using cpu time.
function in file /usr/local/MATLAB/R2011a/toolbox/matlab/datafun/var.m
Copy to new window for comparing multiple runs
Parents (calling functions)
Function Name | Function Type | Calls |
std | function | 1673 |
Lines where the most time was spent
Line Number | Code | Calls | Total Time | % Time | Time Plot |
89 | y = sum(abs(x).^2, dim) ./ den... | 1673 | 0.984 s | 45.0% |  |
87 | x = bsxfun(@minus, x, xbar); | 1673 | 0.874 s | 40.0% |  |
86 | xbar = sum(x, dim) ./ n; | 1673 | 0.284 s | 13.0% |  |
62 | if nargin < 2 || isempty(w)... | 1673 | 0.011 s | 0.5% |  |
88 | end | 1673 | 0 s | 0% |  |
All other lines | | | 0.033 s | 1.5% |  |
Totals | | | 2.186 s | 100% | |
Children (called functions)
No childrenCode Analyzer results
No Code Analyzer messages.Coverage results
[ Show coverage for parent directory ]
Total lines in function | 111 |
Non-code lines (comments, blank lines) | 74 |
Code lines (lines that can run) | 37 |
Code lines that did run | 16 |
Code lines that did not run | 21 |
Coverage (did run/can run) | 43.24 % |
Function listing
time calls line
1 function y = var(x,w,dim)
2 %VAR Variance.
3 % For vectors, Y = VAR(X) returns the variance of the values in X. For
4 % matrices, Y is a row vector containing the variance of each column of
5 % X. For N-D arrays, VAR operates along the first non-singleton
6 % dimension of X.
7 %
8 % VAR normalizes Y by N-1 if N>1, where N is the sample size. This is
9 % an unbiased estimator of the variance of the population from which X is
10 % drawn, as long as X consists of independent, identically distributed
11 % samples. For N=1, Y is normalized by N.
12 %
13 % Y = VAR(X,1) normalizes by N and produces the second moment of the
14 % sample about its mean. VAR(X,0) is the same as VAR(X).
15 %
16 % Y = VAR(X,W) computes the variance using the weight vector W. W typically
17 % contains either counts or inverse variances. The length of W must equal
18 % the length of the dimension over which VAR operates, and its elements must
19 % be nonnegative. If X(I) is assumed to have variance proportional to
20 % 1/W(I), then Y * MEAN(W)/W(I) is an estimate of the variance of X(I). In
21 % other words, Y * MEAN(W) is an estimate of variance for an observation
22 % given weight 1.
23 %
24 % Y = VAR(X,W,DIM) takes the variance along the dimension DIM of X. Pass
25 % in 0 for W to use the default normalization by N-1, or 1 to use N.
26 %
27 % The variance is the square of the standard deviation (STD).
28 %
29 % Example: If X = [4 -2 1
30 % 9 5 7]
31 % then var(X,0,1) is [12.5 24.5 18.0] and var(X,0,2) is [9.0
32 % 4.0]
33 %
34 % Class support for inputs X, W:
35 % float: double, single
36 %
37 % See also MEAN, STD, COV, CORRCOEF.
38
39 % VAR supports both common definitions of variance. If X is a
40 % vector, then
41 %
42 % VAR(X,0) = SUM(RESID.*CONJ(RESID)) / (N-1)
43 % VAR(X,1) = SUM(RESID.*CONJ(RESID)) / N
44 %
45 % where RESID = X - MEAN(X) and N is LENGTH(X). For scalar X,
46 % the first definition would result in NaN, so the denominator N
47 % is always used.
48 %
49 % The weighted variance for a vector X is defined as
50 %
51 % VAR(X,W) = SUM(W.*RESID.*CONJ(RESID)) / SUM(W)
52 %
53 % where now RESID is computed using a weighted mean.
54
55 % Copyright 1984-2010 The MathWorks, Inc.
56 % $Revision: 1.7.4.9 $ $Date: 2010/08/23 23:07:43 $
57
1673 58 if isinteger(x)
59 error(message('MATLAB:var:integerClass'));
60 end
61
0.01 1673 62 if nargin < 2 || isempty(w), w = 0; end
63
1673 64 if nargin < 3
65 % The output size for [] is a special case when DIM is not given.
1473 66 if isequal(x,[]), y = NaN(class(x)); return; end
67
68 % Figure out which dimension sum will work along.
1473 69 dim = find(size(x) ~= 1, 1);
1473 70 if isempty(dim), dim = 1; end
1473 71 end
1673 72 n = size(x,dim);
73
74 % Unweighted variance
1673 75 if isequal(w,0) || isequal(w,1)
1673 76 if w == 0 && n > 1
77 % The unbiased estimator: divide by (n-1). Can't do this
78 % when n == 0 or 1.
1673 79 denom = n - 1;
80 else
81 % The biased estimator: divide by n.
82 denom = n; % n==0 => return NaNs, n==1 => return zeros
83 end
84
1673 85 if n > 0 % avoid divide-by-zero
0.28 1673 86 xbar = sum(x, dim) ./ n;
0.87 1673 87 x = bsxfun(@minus, x, xbar);
1673 88 end
0.98 1673 89 y = sum(abs(x).^2, dim) ./ denom; % abs guarantees a real result
90
91 % Weighted variance
92 elseif isvector(w) && all(w >= 0)
93 if numel(w) ~= n
94 if isscalar(w)
95 error(message('MATLAB:var:invalidWgts'));
96 else
97 error(message('MATLAB:var:invalidSizeWgts'));
98 end
99 end
100
101 % Normalize W, and embed it in the right number of dims. Then
102 % replicate it out along the non-working dims to match X's size.
103 wresize = ones(1,max(ndims(x),dim)); wresize(dim) = n;
104 w = reshape(w ./ sum(w), wresize);
105 x0 = bsxfun(@times, w, x);
106 x = bsxfun(@minus, x, sum(x0, dim));
107 y = sum(bsxfun(@times, w, abs(x).^2), dim); % abs guarantees a real result
108
109 else
110 error(message('MATLAB:var:invalidWgts'));
111 end