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

3 ビュー (過去 30 日間)
Abhishek Chakraborty 2020 年 9 月 1 日
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 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.
##### 1 件のコメント-1 件の古いコメントを表示-1 件の古いコメントを非表示
Abhishek Chakraborty 2020 年 9 月 1 日
Thanks a lot Dana.

サインインしてコメントする。

### カテゴリ

Help Center および File ExchangeCorrelation and Convolution についてさらに検索

R2019b

### Community Treasure Hunt

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

Start Hunting!

Translated by