协方差和相关矩阵之间的差异
Difference in between Covariance and Correlation Matrix
在 Matlab 中,我创建了一个大小为 (244x2014723)
的矩阵 A
和大小为 (244x1)
的矩阵 B
我能够使用 corr(A,B)
计算相关矩阵,它产生了大小为 2014723x1
的矩阵。因此,矩阵 A 的每一列都与矩阵 B 相关联,并在大小为 2014723x1
.
的矩阵中给出一行值
我的问题是,当我使用 cov(A,B)
请求协方差矩阵时,我收到一条错误消息,指出 A 和 B 的大小应该相同。为什么会出现此错误?查找 corr(A,B)
的方法与查找 cov(A,B)
的方法有何不同?
见link
在更多关于部分中,描述如何为 cov(A,B) 计算 cov 的等式清楚地说明了为什么它们需要相同的大小。求和仅针对一个变量,该变量枚举 A、B 的元素。
如果您阅读文档,答案就很清楚了:
cov
:
If A and B are matrices of observations, cov(A,B) treats A and B as vectors and is equivalent to cov(A(:),B(:)). A and B must have equal size.
corr(X,Y) returns a p1-by-p2 matrix containing the pairwise correlation coefficient between each pair of columns in the n-by-p1 and n-by-p2 matrices X and Y.
The difference between corr(X,Y) and the MATLAB® function corrcoef(X,Y) is that corrcoef(X,Y) returns a matrix of correlation coefficients for the two column vectors X and Y. If X and Y are not column vectors, corrcoef(X,Y) converts them to column vectors.
获得向量与矩阵每一列的协方差的一种方法是使用循环。另一种方式(可能效率低下取决于大小)是
C = cov([B,A])
然后查看第一行(或第一列)或C
。
在 Matlab 中,我创建了一个大小为 (244x2014723)
的矩阵 A
和大小为 (244x1)
我能够使用 corr(A,B)
计算相关矩阵,它产生了大小为 2014723x1
的矩阵。因此,矩阵 A 的每一列都与矩阵 B 相关联,并在大小为 2014723x1
.
我的问题是,当我使用 cov(A,B)
请求协方差矩阵时,我收到一条错误消息,指出 A 和 B 的大小应该相同。为什么会出现此错误?查找 corr(A,B)
的方法与查找 cov(A,B)
的方法有何不同?
见link
在更多关于部分中,描述如何为 cov(A,B) 计算 cov 的等式清楚地说明了为什么它们需要相同的大小。求和仅针对一个变量,该变量枚举 A、B 的元素。
如果您阅读文档,答案就很清楚了:
cov
:
If A and B are matrices of observations, cov(A,B) treats A and B as vectors and is equivalent to cov(A(:),B(:)). A and B must have equal size.
corr(X,Y) returns a p1-by-p2 matrix containing the pairwise correlation coefficient between each pair of columns in the n-by-p1 and n-by-p2 matrices X and Y.
The difference between corr(X,Y) and the MATLAB® function corrcoef(X,Y) is that corrcoef(X,Y) returns a matrix of correlation coefficients for the two column vectors X and Y. If X and Y are not column vectors, corrcoef(X,Y) converts them to column vectors.
获得向量与矩阵每一列的协方差的一种方法是使用循环。另一种方式(可能效率低下取决于大小)是
C = cov([B,A])
然后查看第一行(或第一列)或C
。