How to find row wise correlation coefficients and p values for two matrices?
조회 수: 8 (최근 30일)
이전 댓글 표시
I have two matrices, A and B. Both A and B have 3420 rows and 29 columns. I want the correlation coefficients and P values of the correspoding rows of A and B.
I need a result of P value and R value as 3420 rows and 1 column. How to get to it?
I tried the following code:
[R,P]=corr(A',B');
But the result of R and P is a 3420x3420 matrix. How to get R and P as 3420x1 vector for the row wise correlation coefficients between these two matrices A and B?
댓글 수: 0
채택된 답변
Dana
2020년 9월 1일
n = 3420;
R = zeros(n,1);
P = zeros(n,1);
for j = 1:n
[R(j),P(j)]=corr(A(j,:)',B(j,:)');
end
If this is just a one-off thing with the dimensions of the data you've shown, then this will be fine. However, if you're dealing with larger data sets, or performing this calculation many times, you'll be better off calculating this yourself from first principles, rather than using the built-in functions:
k = 29;
dmA = A - mean(A,2);
dmB = B - mean(B,2);
stdA = std(A,1,2);
stdB = std(B,1,2);
R = mean(dmA.*dmB,2)./(stdA.*stdB);
tst = sqrt(k-2)*R./sqrt(1-R.^2);
P = 2*(1-tcdf(abs(tst),k-2));
On my machine, the loop version above takes about 0.3 seconds, whereas this "first principles" version takes 0.003 seconds.
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!