I wrote my own code to find correlation coefficient, but the results are wrong. Why?
14 次查看(过去 30 天)
显示 更早的评论
Hi guys,
I wrote a code to calculate correlation coefficient among my 76x4000 matrix. Basically I want to test for the correlation coefficient within the 76 rows. My end result should be a 76x76 matrix with 1s on the diagonal line and it being symmetric. My codes managed all this, but my result seem wrong, can someone please check it for me please. Thank you.
function T = colerrationcoefficient(P)
for i = 1:76
for j = 1:76
w = 0;
if i~=j
s = cov(P(i,:),P(j,:));
t = s(:,2);
u = std(P(i,:));
v = std(P(j,:));
w = t/(u*v);
end
T = [T;w];
end
end
end
Thank you!
2 个评论
John D'Errico
2016-8-25
Please stop posting the same question. Posting it again won't get more answers.
回答(1 个)
John D'Errico
2016-8-25
Please stop asking the same question multiple times. Anyway, why not just use corrcoef? After all, if you are willing to use cov, why do you feel you need to do the correlation matrix on your own? It is not like you were starting from scratch, and using loops to do it all from just numbers.
So all I can think of is that you don't know that all you had to do was transpose your data matrix.
T = corrcoef(P');
It is rarely a good idea to write code for basic tools that already exist. As a novice, you will always do a worse job than what you will find already written and supplied. So if you want to do something that is fairly common, then first look to see if it already exists.
Yes, you could compute the correlation matrix from the covariance matrix. But if you would do that, then learn to use MATLAB!
C = cov(P');
sinv = 1./std(P');
T = bsxfun(@times,sinv',bsxfun(@times,C,sinv));
In fact, there was no real need to use cov at all. These lines would have done the same to compute the correlation matrix, by standardizing the variables first:
Psub = bsxfun(@rdivide,bsxfun(@minus,P',mu),std(P'));
T = Psub'*Psub/(size(P,2)-1);
Or, I could simply have used cov on the unit normalized array.
If you insist on the use of loops here, then you need to learn to preallocate your final matrix. You would need to learn to assign elements into that matrix, rather than trying to grow it in some random way. But really, learn to use MATLAB. Learn to use arrays & vectors.
2 个评论
John D'Errico
2016-8-25
编辑:John D'Errico
2016-8-25
And my point is, it bloody well DOES work! Did you try what I suggested?
Read the help for corrcoef. What does it generate when you pass it a matrix?
In your case, you have asked to compute the correlation coefficient between ROWS of a matrix. Corrcoef works between the columns.
"R=corrcoef(X) calculates a matrix R of correlation coefficients for
an array X, in which each row is an observation and each column is a
variable."
So why would I have transposed your array? What does a transpose operator do to the rows of a matrix?
Try what I showed you. Then go back and read the help for the various functions used.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!