5
Variance V = var(A) example V = var( ___ ,w) example V = var( ___ ,dim) example V = var( ___ ,nanflag) example example example example example Open This Example collapse all var Syntax Description V = var(A) returns the variance of the elements of A along the first array dimension whose size does not equal 1. If A is a vector of observations, the variance is a scalar. If A is a matrix whose columns are random variables and whose rows are observations, V is a row vector containing the variances corresponding to each column. If A is a multidimensional array, then var(A) treats the values along the first array dimension whose size does not equal 1 as vectors. The size of this dimension becomes 1 while the sizes of all other dimensions remain the same. The variance is normalized by the number of observations‐1 by default. If A is a scalar, var(A) returns 0. If A is a 0by0 empty array, var(A) returns NaN. V = var( ___ ,w) specifies a weighting scheme for any of the previous syntaxes. When w=0 (default), V is normalized by the number of observations‐1. When w=1, it is normalized by the number of observations. w can also be a weight vector containing nonnegative elements. In this case, the length of w must equal the length of the dimension over which var is operating. V = var( ___ ,dim) returns the variance along the dimension dim for any of the previous syntaxes. To maintain the default normalization while specifying the dimension of operation, set w=0 in the second argument. V = var( ___ ,nanflag) specifies whether to include or omit NaN values from the calculation for any of the previous syntaxes. For example, var(A,'includenan') includes all NaN values in A while var(A,'omitnan') ignores them. Examples Variance of Matrix Create a matrix and compute its variance. A = [4 ‐7 3; 1 4 ‐2; 10 7 9]; var(A) ans = 21.0000 54.3333 30.3333

Variance - MATLAB Var

Embed Size (px)

DESCRIPTION

Variance

Citation preview

Page 1: Variance - MATLAB Var

Variance

V = var(A) example

V = var( ___ ,w) example

V = var( ___ ,dim) example

V = var( ___ ,nanflag) example

example

example

example

example

Open This Example

collapse all

var

Syntax

DescriptionV = var(A) returns the variance of the elements of A along the first array dimension whosesize does not equal 1.

If A is a vector of observations, the variance is a scalar.

If A is a matrix whose columns are random variables and whose rows are observations, V isa row vector containing the variances corresponding to each column.

If A is a multidimensional array, then var(A) treats the values along the first arraydimension whose size does not equal 1 as vectors. The size of this dimension becomes 1while the sizes of all other dimensions remain the same.

The variance is normalized by the number of observations‐1 by default.

If A is a scalar, var(A) returns 0. If A is a 0by0 empty array, var(A) returns NaN.

V = var( ___ ,w) specifies a weighting scheme for any of the previous syntaxes. When w = 0(default), V is normalized by the number of observations‐1. When w = 1, it is normalized by thenumber of observations. w can also be a weight vector containing nonnegative elements. In thiscase, the length of w must equal the length of the dimension over which var is operating.

V = var( ___ ,dim) returns the variance along the dimension dim for any of the previoussyntaxes. To maintain the default normalization while specifying the dimension of operation, setw = 0 in the second argument.

V = var( ___ ,nanflag) specifies whether to include or omit NaN values from the calculation forany of the previous syntaxes. For example, var(A,'includenan') includes all NaN values in Awhile var(A,'omitnan') ignores them.

Examples

Variance of Matrix

Create a matrix and compute its variance.

A = [4 ‐7 3; 1 4 ‐2; 10 7 9]; var(A) 

ans = 

   21.0000   54.3333   30.3333 

Page 2: Variance - MATLAB Var

Open This Example

Open This Example

Open This Example

Variance of Array

Create a 3D array and compute its variance.

A(:,:,1) = [1 3; 8 4]; A(:,:,2) = [3 ‐4; 1 2]; var(A) 

ans(:,:,1) = 

   24.5000    0.5000 

ans(:,:,2) = 

     2    18 

Specify Variance Weight Vector

Create a matrix and compute its variance according to a weight vector w.

A = [5 ‐4 6; 2 3 9; ‐1 1 2]; w = [0.5 0.25 0.25]; var(A,w) 

ans = 

    6.1875    9.5000    6.1875 

Specify Dimension for Variance

Create a matrix and compute its variance along the first dimension.

A = [4 ‐2 1; 9 5 7]; var(A,0,1) 

ans = 

   12.5000   24.5000   18.0000 

Compute the variance of A along the second dimension.

var(A,0,2) 

ans = 

     9      4 

Page 3: Variance - MATLAB Var

Open This Example

collapse all

Variance Excluding NaN

Create a vector and compute its variance, excluding NaN values.

A = [1.77 ‐0.005 3.98 ‐2.95 NaN 0.34 NaN 0.19]; V = var(A,'omitnan') 

V = 

    5.1970 

Input Arguments

A — Input arrayvector | matrix | multidimensional array

Input array, specified as a vector, matrix, or multidimensional array.

Data Types: single | double Complex Number Support: Yes

w — Weight0 (default) | 1 | vector

Weight, specified as one of:

0 — normalizes by the number of observations‐1. If there is only one observation, the weight is 1.

1 — normalizes by the number of observations.

a vector made up of nonnegative scalar weights corresponding to the dimension of A along which thevariance is calculated.

Data Types: single | double

dim — Dimension to operate alongpositive integer scalar

Dimension to operate along, specified as a positive integer scalar. If no value is specified, then the defaultis the first array dimension whose size does not equal 1.

Dimension dim indicates the dimension whose length reduces to 1. The size(V,dim) is 1, while the sizesof all other dimensions remain the same.

Consider a twodimensional input array, A.

If dim = 1, then var(A,0,1) returns a row vector containing the variance of the elements in eachcolumn.

Page 4: Variance - MATLAB Var

collapse all

If dim = 2, then var(A,0,2) returns a column vector containing the variance of the elements in eachrow.

var returns an array of zeros the same size as A when dim is greater than ndims(A).

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

nanflag — NaN condition‘includenan' (default) | ‘omitnan'

NaN condition, specified as one of these values:

‘includenan' — the variance of input containing NaN values is also NaN.

‘omitnan' — all NaN values appearing in either the input array or weight vector are ignored.

Data Types: char

More About

Variance

For a random variable vector A made up of N scalar observations, the variance is defined as

where μ is the mean of A,

Some definitions of variance use a normalization factor of N instead of N1, which can be specified bysetting w to 1. In either case, the mean is assumed to have the usual normalization factor N.

See Alsocorrcoef | cov | mean | std

Introduced before R2006a

V = 1N −1

N

i=1Ai − μ

2

μ = 1N

N

i=1Ai .

Page 5: Variance - MATLAB Var