Why do these two codes give different covariance matrices?

1 次查看(过去 30 天)
1st code:
S =[ 0.6661 - 0.7458i 0.4210 - 0.9071i 1.0000 + 0.0000i;-0.9127 + 0.4086i -0.8429 + 0.5381i 1.0000 + 0.0000i;1.0000 + 0.0000i 1.0000 + 0.0000i 1.0000 + 0.0000i;-0.9127 - 0.4086i -0.8429 - 0.5381i 1.0000 - 0.0000i;0.6661 + 0.7458i 0.4210 + 0.9071i 1.0000 - 0.0000i];
Rmm=cov(S);
2nd code:
S =[ 0.6661 - 0.7458i 0.4210 - 0.9071i 1.0000 + 0.0000i;-0.9127 + 0.4086i -0.8429 + 0.5381i 1.0000 + 0.0000i;1.0000 + 0.0000i 1.0000 + 0.0000i 1.0000 + 0.0000i;-0.9127 - 0.4086i -0.8429 - 0.5381i 1.0000 - 0.0000i;0.6661 + 0.7458i 0.4210 + 0.9071i 1.0000 - 0.0000i];
Rmm= S*S'/length(S(1,:));

回答(1 个)

David Goodmanson
David Goodmanson 2021-5-5
Hi Sadiq,
cov subtracts mean from each column of S, and also divides by (size(S,1) -1). Also S and S' have to be multiplied in the correct order.
S =[ 0.6661 - 0.7458i 0.4210 - 0.9071i 1.0000 + 0.0000i;
-0.9127 + 0.4086i -0.8429 + 0.5381i 1.0000 + 0.0000i;
1.0000 + 0.0000i 1.0000 + 0.0000i 1.0000 + 0.0000i;
-0.9127 - 0.4086i -0.8429 - 0.5381i 1.0000 - 0.0000i;
0.6661 + 0.7458i 0.4210 + 0.9071i 1.0000 - 0.0000i];
Rmm = cov(S)
S1 = S-mean(S);
Rmm1= S1'*S1/(size(S1,1)-1)
  3 个评论
David Goodmanson
David Goodmanson 2021-5-8
编辑:David Goodmanson 2021-5-8
Well, the second code is actually not correct, and the different size of Rmm compared to cov(S) is one indication.
David Goodmanson
David Goodmanson 2021-5-9
编辑:David Goodmanson 2021-5-10
Actually it's pretty easy to say the second code is wrong, since it gives totally different results than Matlab cov which is correct.
Besides which, youtube videos are hardly infallabe. Anyone can post one, and a few are wrong, so you have to take them with a grain of salt. This video is not incorrect, but I would say it is very misleading.
In the video, his table is in the standard form with samples down the columns, variables across the rows. For each variable, its samples form a column vector. The key is that the covariance between two variables is the inner product, a scalar quantity. For two columns of sample data g and h, the inner product is g' * h [matrix multiplication of row g x column h].
However, for purposes of illustration he enters the data as row vectors g and h and makes no comment about the distinction. Then the inner product is expressed the other way round, g * h' .
Since S has its samples in columns, you can see which multiplication is required

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by