Clear Filters
Clear Filters

I wrote my own code to find correlation coefficient, but the results are wrong. Why?

3 views (last 30 days)
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!

Answers (1)

John D'Errico
John D'Errico on 25 Aug 2016
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 Comments
Lakyn
Lakyn on 25 Aug 2016
Hi thanks for the reply. Yes there is a reason why I do not use corrcoef, because it does not work in this case where it needs to be applied so many times in a matrix.
I am learning to use MATLAB and thats why I have so many problems. I don't really understand how the transposing my matrix will help in this case?
And I would like to try your way, but what do you mean by just using normalized array. Should I just make my matrix into vector arrays and use corrcoef?
John D'Errico
John D'Errico on 25 Aug 2016
Edited: John D'Errico on 25 Aug 2016
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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!