var

Variance of the elements of an array.

Category: Language Fundamentals > Data Types

Syntax

v = var(A)

Variance along the first non-singleton dimension, normalized by N-1.

v = var(A, w)

Uses weight 0 (divide by N-1) or 1 (divide by N).

v = var(A, w, dim)

Operates along dimension dim.

v = var(___, 'omitnan')

Ignores NaN values.

Description

The variance is the mean squared distance from the mean. With the default w = 0 the sum is divided by N-1, which gives an unbiased estimate when the data are a sample; w = 1 divides by N, the right choice when the data are the whole population.

A scalar has variance 0. For a matrix, var(A) returns a row with the variance of each column. The standard deviation is sqrt(var(A)), which is std.

Input arguments

A
Numeric or logical vector, matrix or N-D array.
w
0 for the sample variance (default), 1 for the population variance, or a vector of weights.
dim
Dimension to operate along.

Output arguments

v
Variance, with the operating dimension reduced to size 1.

Example

Sample, population and per column

x = [2 4 4 4 5 5 7 9];
var(x)
var(x, 1)
var([1 2; 3 8])
Output
ans = 4.5714
ans = 4
ans = 1×2

     2    18

See also

std mean median cov mode