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

7 Ansichten (letzte 30 Tage)
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?

Akzeptierte Antwort

Dana
Dana am 1 Sep. 2020
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.

Weitere Antworten (0)

Produkte


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by