How to find row wise correlation coefficients and p values for two matrices?

조회 수: 14 (최근 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?

채택된 답변

Dana
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개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by