简体   繁体   中英

Pairwise correlation of matrix rows and columns in Matlab using corr

I have two matrices r1 and r3 of size 32x15 in Matlab . For each matrix, the first dimension 32 refers to 32 "channels" while 15 is subjects. I want to perform pairwise correlation between the matrices such that I end up with a 32x32 matrix which corresponds to correlations between each row and column of the two matrices.

I have so far tried to use corr which works just fine if I want to correlate the matrix with itself but as I understand from the Matlab documentation, corr performs correlations between the columns of the matrices only.

I have tried to the following code using for loops but it doesn't work. If it worked correctly, I would have a perfect correlation along the diagonal, right?

for i=1:32 
 for j=1:32 
    correlation(i,j) = corr(r1(:,i)',r3(:,j)','rows','pairwise'); 
 end
end

Here are two example matrices - they are 7x3 as I could not provide the full 32x15 matrices. If done correctly, I should end up with a 7x7 matrix where each row of r1 has been correlated to each column of r3 and vice versa to give every possible combination of rows and columns. Just to note - my full 32x15 matrices have some NaN values that's why I have used 'rows','pairwise' in the code above.

r1 = 
[0.000785128677421817   0.00151819636200844 0.0201580080422612
0.000789571883743766    0.00235171583502779 0.0168791069201603
0.00291703428671911 -0.00391692099137886    0.389913575774990
-0.0189599881914021 0.000764347722400059    0.0372997553267858
0.00252968249918353 0.00241629437619394 0.0107340950613735
0.00194727745704261 0.000947350671267538    0.0232418511965885
0.00232493840698227 0.00121284469605609 0.0149895778571818]; 

r3 = 
[-7.58811154445689e-05  0.000167510763757933    0.000976987731364720
0.000102649058710682    0.000306092926800082    0.000923635482019152
8.39820669006047e-05    -8.66299116079176e-05   0.0122619540661600
-0.00138041229808295    7.09387885272107e-05    0.00263790111499764
5.24134049463102e-05    5.61596345827880e-05    0.000913040048122104
-0.000150986748979111   4.69321863849151e-05    0.00150323534771508
7.35090129997503e-06    -3.96953623387051e-05   0.000765577840491432];

Any help with this would be really appreciated. I think perhaps I am not understanding how the corr function works exactly which is adding to my confusion.

EDIT: I have also just tried the following after reading this thread: https://uk.mathworks.com/matlabcentral/answers/348781-how-can-i-run-correlations-from-one-row-across-several-rows-in-another-matrix

corra = corr([r11,r33]);

And this gives me a 64x64 matrix which is symmetrical and the diagonal has 1's. Have I misunderstood something by expecting my correlation matrix to be size 32x32 ?

Thanks!

I want to perform pairwise correlation between the matrices such that I end up with a 32x32 matrix which corresponds to correlations between each row and column of the two matrices.

Is what you mean here that you want the first row of the output correlation matrix to be the correlation of the first row of r1 with each row of r3 ?

corr performs correlations between the columns of the matrices only

Matlab has a two argument version corr(X,Y) . If X is (m,n) and Y is (m,p) the output will be shape (n,p) (ie it computes all pairs of correlations of the columns of X and Y ).

If it worked correctly, I would have a perfect correlation along the diagonal, right?

Unless r1 is r3 , this almost certainly wouldn't happen. The correlation of the first column of r1 with itself is certainly 1. The correlation of the first column of r1 with the first column of r3 could be anything between -1 and 1.

Based on what you've written, I think the code should be

corr_mat = corr(r1', r3', 'Rows', 'pairwise');

This will give you a (32,32) matrix where corr_mat(i,j) is the Pearson correlation of channel i in r1 with channel j in r3 . Whether this is meaningful or not I don't know.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM