Converting Covariance Matrix to Correlation Matrix
7 ビュー (過去 30 日間)
古いコメントを表示
I know of MATLAB function to convert a covariance matrix to a correlation matrix but I'm a teacher and want to demonstrate matrix manipulation. I can do the conversion using for loops as below but can't figure out how to do this more elegantly (using matrix operations).
d=diag(cov).^.5;
for i=1:length(cov)
for j=1:length(cov)
corr(i,j)=cov(i,j)./(d(i)*d(j));
end
end
Thanks in advance for any help.
0 件のコメント
回答 (3 件)
Peter Perkins
2014 年 10 月 14 日
Jim, if you mean, "matrix operations" as in "something out of a math book", then you can right and left multiply by diag(d), where d is 1./sqrt(diag(C)). If you mean "MATLAB matrix operations", then you can use ./ with d*d'.
You might find it interesting to look at the guts of corrcoef, where the code takes a bit more care.
Hope this helps.
2 件のコメント
Peter Perkins
2014 年 10 月 24 日
Sorry, I think I meant diag(1./sqrt(diag(C))). If C is a 3x3 cov matrix, then diag(C) is a vector, and diag(1./sqrt(diag(C))) is a 3x3 diagonal matrix.
James Tursa
2014 年 10 月 24 日
編集済み: James Tursa
2014 年 10 月 24 日
A method using outer product and element-wise divide:
d = sqrt(diag(cov));
corr = cov./(d*d');
Matt J
2014 年 10 月 24 日
corr=bsxfun(@rdivide,cov, d(:));
corr=bsxfun(@rdivide, corr,d(:).');
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!