在matlab中求协方差,matlab里面的求协方差函数
一:matlab里面關(guān)于方差和協(xié)方差的幾點需要注意的
求均值的函數(shù):mean
求方差的函數(shù):var
標(biāo)準(zhǔn)差:
方差:
求協(xié)方差的函數(shù):cov
1 如果a,b是向量,則:
cov(a,b)相當(dāng)于是[D(a),cov(a,b);cov(b,a),D(b)],其中D(a)和D(b)分別代表了a,b的方差。cov(a,b)和cov(b,a)分別代表了兩者之間的協(xié)方差
舉例:
>> a=[1.1 2.2 3 4.5 0.8]
a =
1.1000 2.2000 3.0000 4.5000 0.8000
>> b=[1.3 2.4 2.1 5 3.3]
b =
1.3000 2.4000 2.1000 5.0000 3.3000
>> cov(a,b)
ans =
2.2570 1.3595
1.3595 1.9970
其中,對角線的值是兩個向量的方差,斜對角線的是兩個向量之間的協(xié)方差
2 如果a,b是矩陣,則:
cov(a,b)相當(dāng)于是cov(a(:),b(:)),舉例說明:
>> a=randn(4);
>> a
a =
-0.4326 -1.1465 0.3273 -0.5883
-1.6656 1.1909 0.1746 2.1832
0.1253 1.1892 -0.1867 -0.1364
0.2877 -0.0376 0.7258 0.1139
>> b=randn(4);
>> b
b =
1.0668 0.2944 -0.6918 -1.4410
0.0593 -1.3362 0.8580 0.5711
-0.0956 0.7143 1.2540 -0.3999
-0.8323 1.6236 -1.5937 0.6900
>> cov(a,b)
ans =
0.8513 -0.0860
-0.0860 1.0129
/
>> a(:)
ans =
-0.4326
-1.6656
0.1253
0.2877
-1.1465
1.1909
1.1892
-0.0376
0.3273
0.1746
-0.1867
0.7258
-0.5883
2.1832
-0.1364
0.1139
>> b(:)
ans =
1.0668
0.0593
-0.0956
-0.8323
0.2944
-1.3362
0.7143
1.6236
-0.6918
0.8580
1.2540
-1.5937
-1.4410
0.5711
-0.3999
0.6900
>> cov(a(:),b(:))
ans =
0.8513 -0.0860
-0.0860 1.0129
另外,matlab里面關(guān)于cov函數(shù)的說明解釋的很詳細:
cov(x), if x is a vector, returns the variance of x. For matrix input X, where each row is an observation, and each column is a variable, cov(X) is the covariance matrix. diag(cov(X)) is a vector of variances for each column, and sqrt(diag(cov(X))) is a vector of standard deviations. cov(X,Y), where X and Y are matrices with the same number of elements, is equivalent to cov([X(:) Y(:)]).
cov(x) or cov(x,y) normalizes by N – 1, if N > 1, where N is the number of observations. This makes cov(X) the best unbiased estimate of the covariance matrix if the observations are from a normal distribution. For N = 1, cov normalizes by N.
cov(x,1) or cov(x,y,1) normalizes by N and produces the second moment matrix of the observations about their mean. cov(X,Y,0) is the same as cov(X,Y) and cov(X,0) is the same as cov(X).
參考:
http://www.voidcn.com/article/p-qzfcdcih-bv.html
總結(jié)
以上是生活随笔為你收集整理的在matlab中求协方差,matlab里面的求协方差函数的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: datapumpdir oracle_o
- 下一篇: typescript的类型描述_一文学懂