To calculate the variance of a vector in Matlab, you can use the var
function. The var
function returns the sample variance of a vector. If you want to calculate the population variance, you need to specify the 1
as the second argument to the function. Here is an example:
main.m196 chars9 lines
In the code above, the randn
function creates a vector of 10 random numbers. The var
function is then used to calculate the sample variance of the vector and store the result in the variable variance
. Finally, the population variance of the vector is calculated by specifying 1
as the second argument to the var
function and storing the result in the variable pop_variance
.
gistlibby LogSnag