MATLAB 中两个矩阵之间的协方差
我有两个矩阵,X 和 Y,每列代表随机变量的多个实现;
X = [x_11 x_21 .... x_n1
x_12 x_22 .... x_n2
. . .... .
. . .... .
x_1m x_2m .... x_nm]
其中 Y 是 X 的函数: Y = f(X)
Y = [y_11 y_21 .... y_n1
y_12 y_22 .... y_n2
. . .... .
. . .... .
y_1m y_2m .... y_nm]
我想找到变量 x_n 和 y_n 之间的协方差矩阵;
E{(X - E{Y}) * (Y - E{Y})^H}
其中 ()^H 表示向量的 厄米转置
在 matlab 中,当我运行 cov(X,Y)
在矩阵上,(每 1000 次试验 20 个变量)我只得到一个 2x2 矩阵,这让我相信它将每个矩阵视为单个矩阵不知何故“变量”。如果我连接两个矩阵并对结果调用 cov
:
cov( [X Y] )
我得到一个 40x40 矩阵,cov( X )
的结果位于左上角,< code>cov( Y ) 在右下角,我想要的矩阵在右上角和左下角,但是有没有一种方法可以计算这个而不必求助于这个?
谢谢
I have two matricies, X and Y, with each column representing multiple realizations of a random variable;
X = [x_11 x_21 .... x_n1
x_12 x_22 .... x_n2
. . .... .
. . .... .
x_1m x_2m .... x_nm]
And where Y is a function of X: Y = f(X)
Y = [y_11 y_21 .... y_n1
y_12 y_22 .... y_n2
. . .... .
. . .... .
y_1m y_2m .... y_nm]
I want to find the covariance matrix between the variables x_n and y_n;
E{(X - E{Y}) * (Y - E{Y})^H}
Where ()^H denotes the Hermitian Transpose of the vector
In matlab, when I run cov(X,Y)
on the matricies, (each 1000 trials of 20 variables) I only get a 2x2 matrix back, which leads me to believe that it is treating each matrix as a single "variable" somehow. If I concatenate the two matricies and call cov
on the result:
cov( [X Y] )
I get a 40x40 matrix, with the result of cov( X )
in the top left, the result of cov( Y )
in the bottom right, and the matrix I want in the top right and bottom left, but is there a way to calculate this without having to resort to this?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
cov(X,Y)
相当于cov([x(:) y(:)])
。但是[x(:) y(:)]
对您来说是 20000 x 2,并且cov()
将行视为观察值,将列视为维度,因此您得到 2通过 2 个协方差矩阵。我会根据定义自己实现它:
如果您有不支持
bsxfun()
的旧版本 matlab,只需使用repmat()
。cov(X,Y)
is equivalent tocov([x(:) y(:)])
. But[x(:) y(:)]
is 20000 by 2 for you, andcov()
treats rows as observations and columns as dimensions, so you get a 2 by 2 covariance matrix.I would just implement it myself by the definition:
If you have an older version of matlab that doesn't support
bsxfun()
, just userepmat()
.