在matlab中求协方差,matlab里面的求协方差函数

本文详细介绍了在MATLAB中如何使用mean、var、cov等函数计算方差和协方差。当向量作为输入时,cov函数返回协方差矩阵,对角线元素为方差,斜对角线元素为两个向量之间的协方差。对于矩阵输入,cov函数处理每一列作为变量的情况。此外,还讨论了不同参数设置对结果的影响,包括正常化方式的选择。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一:matlab里面关于方差和协方差的几点需要注意的

求均值的函数:mean

0818b9ca8b590ca3270a3433284dd417.png

求方差的函数:var

标准差:

0818b9ca8b590ca3270a3433284dd417.png

方差:

0818b9ca8b590ca3270a3433284dd417.png

求协方差的函数:cov

0818b9ca8b590ca3270a3433284dd417.png

1 如果a,b是向量,则:

cov(a,b)相当于是[D(a),cov(a,b);cov(b,a),D(b)],其中D(a)和D(b)分别代表了a,b的方差。cov(a,b)和cov(b,a)分别代表了两者之间的协方差

举例:

>> 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

其中,对角线的值是两个向量的方差,斜对角线的是两个向量之间的协方差

2 如果a,b是矩阵,则:

cov(a,b)相当于是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里面关于cov函数的说明解释的很详细:

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值