How to calculate the correlation coefficient between an array and a matrix?

조회 수: 17 (최근 30일)
Nut
Nut 2016년 5월 25일
댓글: Nut 2016년 7월 1일
Hi,
I have a matrix A and a matrix B, with the same number of rows and a different number of columns. I need to calculate the correlation coefficient between each single columns of the matrix A and all the columns of the matrix B. For each column of A, the partial result will be an array, so I'm thinking to a matrix as final result.
Is there a way to do this avoiding the "for" cycle? Which is the most efficient way to do this? Could you suggest me the best syntax?
Finally, I have also to do the same with the mean squared error: again, in this second case, is it possible to avoid the "for" cycle?
Thanks for your answers.

채택된 답변

Tom Lane
Tom Lane 2016년 6월 24일
If you have the Statistics and Machine Learning Toolbox, it sounds like you want this:
>> x = randn(20,3);
>> y = x*[1 0;0 1;1 1];
>> corr(x,y)
ans =
0.9221 -0.1434
-0.2979 0.8438
0.6825 0.5606
I'm not sure what you mean by mean squared error. The following adds some noise to get z, then computes coefficients for predicting y from z, then computes the sum of squared differences between y and the predicted values for each column. Does this point you in the right direction?
>> z = x+randn(size(x))/100;
>> b
b =
0.9983 -0.0009
-0.0000 0.9964
1.0016 1.0049
>> sum((y-yhat).^2)
ans =
0.0025 0.0054
  댓글 수: 1
Nut
Nut 2016년 7월 1일
Thank you very much, Tom. corr is what I needed. Also what you said about mean squared error can help me, but more precisely I was wondering about a way to calculate it between each pairs of columns of the two matrices.
I would like to know if there is a way similar to "corr", to get a similar matrix containing the results for each pair, avoiding a "for" loop.
Thank you again!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

Community Treasure Hunt

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

Start Hunting!

Translated by