Variance - MATLAB Var
Variance - MATLAB Var
Variance
Syntax
V=var(A)
example
example
example
example
Description
V=var(A)returnsthevarianceoftheelementsofAalongthefirstarraydimensionwhose
sizedoesnotequal1.
IfAisavectorofobservations,thevarianceisascalar.
IfAisamatrixwhosecolumnsarerandomvariablesandwhoserowsareobservations,Vis
arowvectorcontainingthevariancescorrespondingtoeachcolumn.
IfAisamultidimensionalarray,thenvar(A)treatsthevaluesalongthefirstarray
dimensionwhosesizedoesnotequal1asvectors.Thesizeofthisdimensionbecomes1
whilethesizesofallotherdimensionsremainthesame.
Thevarianceisnormalizedbythenumberofobservations1bydefault.
IfAisascalar,var(A)returns0.IfAisa0by0emptyarray,var(A)returnsNaN.
example
example
example
example
Examples
collapseall
VarianceofMatrix
Createamatrixandcomputeitsvariance.
A=[473;142;1079];
var(A)
ans=
21.000054.333330.3333
OpenThisExample
VarianceofArray
Createa3Darrayandcomputeitsvariance.
OpenThisExample
A(:,:,1)=[13;84];
A(:,:,2)=[34;12];
var(A)
ans(:,:,1)=
24.50000.5000
ans(:,:,2)=
218
SpecifyVarianceWeightVector
Createamatrixandcomputeitsvarianceaccordingtoaweightvectorw.
OpenThisExample
A=[546;239;112];
w=[0.50.250.25];
var(A,w)
ans=
6.18759.50006.1875
SpecifyDimensionforVariance
Createamatrixandcomputeitsvariancealongthefirstdimension.
A=[421;957];
var(A,0,1)
ans=
12.500024.500018.0000
ComputethevarianceofAalongtheseconddimension.
var(A,0,2)
ans=
9
4
OpenThisExample
VarianceExcludingNaN
Createavectorandcomputeitsvariance,excludingNaNvalues.
OpenThisExample
A=[1.770.0053.982.95NaN0.34NaN0.19];
V=var(A,'omitnan')
V=
5.1970
InputArguments
collapseall
AInputarray
vector|matrix|multidimensionalarray
Inputarray,specifiedasavector,matrix,ormultidimensionalarray.
DataTypes:single|double
ComplexNumberSupport:Yes
wWeight
0(default)|1|vector
Weight,specifiedasoneof:
0normalizesbythenumberofobservations1.Ifthereisonlyoneobservation,theweightis1.
1normalizesbythenumberofobservations.
avectormadeupofnonnegativescalarweightscorrespondingtothedimensionofAalongwhichthe
varianceiscalculated.
DataTypes:single|double
dimDimensiontooperatealong
positiveintegerscalar
Dimensiontooperatealong,specifiedasapositiveintegerscalar.Ifnovalueisspecified,thenthedefault
isthefirstarraydimensionwhosesizedoesnotequal1.
Dimensiondimindicatesthedimensionwhoselengthreducesto1.Thesize(V,dim)is1,whilethesizes
ofallotherdimensionsremainthesame.
Consideratwodimensionalinputarray,A.
Ifdim=1,thenvar(A,0,1)returnsarowvectorcontainingthevarianceoftheelementsineach
column.
Ifdim=2,thenvar(A,0,2)returnsacolumnvectorcontainingthevarianceoftheelementsineach
row.
varreturnsanarrayofzerosthesamesizeasAwhendimisgreaterthanndims(A).
DataTypes:single|double|int8|int16|int32|int64|uint8|uint16|uint32|uint64
nanflagNaNcondition
includenan'(default)|omitnan'
NaNcondition,specifiedasoneofthesevalues:
includenan'thevarianceofinputcontainingNaNvaluesisalsoNaN.
omitnan'allNaNvaluesappearingineithertheinputarrayorweightvectorareignored.
DataTypes:char
MoreAbout
collapseall
Variance
ForarandomvariablevectorAmadeupofNscalarobservations,thevarianceisdefinedas
1
N 1
V =
i =1
Ai
whereisthemeanofA,
1
N
i =1
Ai .
SomedefinitionsofvarianceuseanormalizationfactorofNinsteadofN1,whichcanbespecifiedby
settingwto1.Ineithercase,themeanisassumedtohavetheusualnormalizationfactorN.
SeeAlso
corrcoef|cov|mean|std
IntroducedbeforeR2006a