Matlab中,有一个var函数,可以计算数组的方差。本文将从var函数基本用法、var函数用法介绍、var函数实例等几个方面入手介绍var函数。其中实例主要包括:矩阵的方差、三维阵列的方差、指定权重向量计算方差、指定维度计算方差、不含NaN的方差。
方差是在概率论和统计方差衡量随机变量或一组数据时离散程度的度量。概率论中方差用来度量随机变量和其数学期望(即均值)之间的偏离程度。统计中的方差(样本方差)是每个样本值与全体样本值的平均数之差的平方值的平均数。在许多实际问题中,研究方差即偏离程度有着重要意义。
var函数的帮助文档如下:
>> help var
var Variance.
For vectors, Y = var(X) returns the variance of the values in X. For
matrices, Y is a row vector containing the variance of each column of
X. For N-D arrays, var operates along the first non-singleton
dimension of X.
var normalizes Y by N-1 if N>1, where N is the sample size. This is
an unbiased estimator of the variance of the population from which X is
drawn, as long as X consists of independent, identically distributed
samples. For N=1, Y is normalized by N.
Y = var(X,1) normalizes by N and produces the second moment of the
sample about its mean. var(X,0) is the same as var(X).
Y = var(X,W) computes the variance using the weight vector W. W
typically contains either counts or inverse variances. The length of W
must equal the length of the dimension over which var operates, and its
elements must be nonnegative. If X(I) is assumed to have variance
proportional to 1/W(I), then Y * MEAN(W)/W(I) is an estimate of the
variance of X(I). In other words, Y * MEAN(W) is an estimate of
variance for an observation given weight 1.
Y = var(X,W,DIM) takes the variance along the dimension DIM of X. Pass
in 0 for W to use the default normalization by N-1, or 1 to use N.
The variance is the square of the standard deviation (STD).
var(..., MISSING) specifies how NaN (Not-A-Number) values are treated.
The default is 'includenan':
'includenan' - the variance of a vector containing NaN values
is also NaN.
'omitnan' - elements of X or W containing NaN values are ignored.
If all elements are NaN, the result is NaN.
Example: If X = [4 -2 1; 9 5 7],
then var(X,0,1) is [12.5 24.5 18.0] and var(X,0,2) is [9.0; 4.0].
var函数基本用法
V = var(A)
V = var(___,w)
V = var(___,dim)
V = var(___,nanflag)
var函数用法介绍
V=var(A)返回A元素沿第一个数组维度的方差,该数组维度的大小不等于1。
- 如果A是观测向量,则方差是标量。
- 如果A是一个矩阵,其列是随机变量,其行是观察值,则V是一个行向量,其中包含与每列对应的方差。
- 如果A是多维数组,则var(A)将第一个数组维度上大小不等于1的值视为向量。此维度的大小变为1,而所有其他维度的大小保持不变。
- 默认情况下,方差通过观察次数-1进行归一化。
- 如果A是标量,var(A)返回0。如果A是一个0乘0的空数组,var(A)返回NaN。
V=var(_,w)指定了前面任何语法的加权方案。当w=0(默认值)时,V通过观察次数-1进行归一化。当w=1时,V根据观察次数进行归一化。w也可以是包含非负元素的权重向量。在这种情况下,w的长度必须等于var操作的维度的长度。
V=var(_,dim)返回前面任何语法沿维度dim的方差。要在指定操作维度时保持默认规范化,请在第二个参数中设置w=0。
V=var(_,nanflag)指定是否在前面任何语法的计算中包含或忽略NaN值。例如,var(A,’includenan’)包含A中的所有NaN值,而var(A,’omitnan’)忽略它们。
var函数实例
矩阵的方差
创建一个矩阵并计算其方差。
>> A = [4 -7 3; 1 4 -2; 10 7 9];
>> var(A)
ans =
21.0000 54.3333 30.3333
三维阵列的方差
创建三维数组并计算其方差。
>> A(:,:,1) = [1 3; 8 4];
>> A(:,:,2) = [3 -4; 1 2];
>> var(A)
ans(:,:,1) =
24.5000 0.5000
ans(:,:,2) =
2 18
指定权重向量计算方差
创建一个矩阵,并根据权重向量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
指定维度计算方差
创建一个矩阵并沿第一维度计算其方差。
>> A = [4 -2 1; 9 5 7];
>> var(A,0,1)
ans =
12.5000 24.5000 18.0000
计算A沿第二维度的方差。
>> var(A,0,2)
ans =
9
4
不含NaN的方差
创建一个向量并计算其方差,不包括NaN值。
>> A = [1.77 -0.005 3.98 -2.95 NaN 0.34 NaN 0.19];
>> V = var(A,'omitnan')
V =
5.1970
原创文章,作者:古哥,转载需经过作者授权同意,并附上原文链接:https://iymark.com/articles/3847.html