Function Reference: var

statistics: v = var (x)
statistics: v = var (x, w)
statistics: v = var (x, w, "all")
statistics: v = var (x, w, dim)
statistics: v = var (x, w, vecdim)
statistics: v = var (…, nanflag)
statistics: [v, m] = var (…)

Compute the variance of the elements of x.

  • If x is a vector, then var(x) returns the variance of the elements in x defined as $$ {\rm var}(x) = {1\over N-1} \sum_{i=1}^N |x_i - \bar x |^2 $$ where N is the length of the x vector.
  • If x is a matrix, then var (x) returns a row vector with the variance of each column in x.
  • If x is a multi-dimensional array, then var (x) operates along the first non-singleton dimension of x.

var (x, w) specifies a weighting scheme. When w = 0 (default), the variance is normalized by N-1 (population variance) where N is the number of observations. When w = 1, the variance is normalized by the number of observations (sample variance). To use the default value you may pass an empty input argument [] before entering other options.

w can also be an array of non-negative numbers. When w is a vector, it must have the same length as the number of elements in the operating dimension of x. If w is a matrix or n-D array, or the operating dimension is supplied as a vecdim or "all", w must be the same size as x. NaN values are permitted in w, will be multiplied with the associated values in x, and can be excluded by the nanflag option.

var (x, [], dim) returns the variance along the operating dimension dim of x. For dim greater than ndims (x) v is returned as zeros of the same size as x and m = x.

var (x, [], vecdim) returns the variance over the dimensions specified in the vector vecdim. For example, if x is a 2-by-3-by-4 array, then var (x, [1 2]) returns a 1-by-1-by-4 array. Each element of the output array is the variance of the elements on the corresponding page of x. If vecdim indexes all dimensions of x, then it is equivalent to var (x, "all"). Any dimension in vecdim greater than ndims (x) is ignored.

var (x, "all") returns the variance of all the elements in x. The optional flag "all" cannot be used together with dim or vecdim input arguments.

var (…, nanflag) specifies whether to exclude NaN values from the calculation using any of the input argument combinations in previous syntaxes. The default value for nanflag is "includenan", and keeps NaN values in the calculation. To exclude NaN values, set the value of nanflag to "omitnan".

[v, m] = var (…) also returns the mean of the elements of x used to calculate the variance. If v is the weighted variance, then m is the weighted mean.

See also: std, mean

Source Code: var