Ais a vector of observations, the variance is a scalar.
Ais a matrix whose columns are random variables and whose rows are observations,
Vis a row vector containing the variances corresponding to each column.
Ais 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
1while the sizes of all other dimensions remain the same.
The variance is normalized by the number of observations
Ais a scalar,
V = var( specifies
a weighting scheme. When
w = 0 (default),
normalized by the number of observations
= 1, it is normalized by the number of observations.
also be a weight vector containing nonnegative elements. In this case,
the length of
w must equal the length of the dimension
var is operating.
V = var(
computes the variance over the dimensions specified in the vector
w is 0 or 1. For example, if
A is a matrix, then
computes the variance over all elements in
A, since every element
of a matrix is contained in the array slice defined by dimensions 1 and 2.
Variance of Matrix
Create a matrix and compute its variance.
A = [4 -7 3; 1 4 -2; 10 7 9]; var(A)
ans = 1×3 21.0000 54.3333 30.3333
Variance of Array
Create a 3-D array and compute its variance.
A(:,:,1) = [1 3; 8 4]; A(:,:,2) = [3 -4; 1 2]; var(A)
ans = 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
A = [5 -4 6; 2 3 9; -1 1 2]; w = [0.5 0.25 0.25]; var(A,w)
ans = 1×3 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 = 1×3 12.5000 24.5000 18.0000
Compute the variance of
A along the second dimension.
ans = 2×1 9 4
Variance of Array Page
Create a 3-D array and compute the variance over each page of data (rows and columns).
A(:,:,1) = [2 4; -2 1]; A(:,:,2) = [9 13; -5 7]; A(:,:,3) = [4 4; 8 -3]; V = var(A,0,[1 2])
V = V(:,:,1) = 6.2500 V(:,:,2) = 60 V(:,:,3) = 20.9167
Create a vector and compute its variance, excluding
A = [1.77 -0.005 3.98 -2.95 NaN 0.34 NaN 0.19]; V = var(A,'omitnan')
V = 5.1970
A — Input array
vector | matrix | multidimensional array
Input array, specified as a vector, matrix, or multidimensional array.
Complex Number Support: Yes
w — Weight
0 (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
Aalong which the variance is calculated.
dim — Dimension to operate along
positive integer scalar
Dimension to operate along, specified as a positive integer scalar. If no value is specified, then the default is the first array dimension whose size does not equal 1.
dim indicates the dimension whose
length reduces to
while the sizes of all other dimensions remain the same.
Consider a two-dimensional input array,
dim = 1, then
var(A,0,1)returns a row vector containing the variance of the elements in each column.
dim = 2, then
var(A,0,2)returns a column vector containing the variance of the elements in each row.
var returns an array of zeros the same size
dim is greater than
vecdim — Vector of dimensions
vector of positive integers
Vector of dimensions, specified as a vector of positive integers. Each element represents a dimension of the input array. The lengths of the output in the specified operating dimensions are 1, while the others remain the same.
Consider a 2-by-3-by-3 input array,
var(A,0,[1 2]) returns a 1-by-1-by-3 array whose
elements are the variances computed over each page of
'includenan' (default) |
NaN condition, specified as one of these
'includenan'— the variance of input containing
NaNvalues is also
NaNvalues appearing in either the input array or weight vector are ignored.
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 N-1, which can be specified by setting
In either case, the mean is assumed to have the usual normalization
Calculate with arrays that have more rows than fit in memory.
This function supports tall arrays with the limitation:
The weighting scheme cannot be a vector.
For more information, see Tall Arrays for Out-of-Memory Data.
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
dimmust be a constant.
See Variable-Sizing Restrictions for Code Generation of Toolbox Functions (MATLAB Coder).
GPU Code Generation
Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.
Usage notes and limitations:
dimmust be a constant.
Run code in the background using MATLAB®
backgroundPool or accelerate code with Parallel Computing Toolbox™
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.
This function fully supports GPU arrays. For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.
This function fully supports distributed arrays. For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).