- Transpose your matrices on the way in to corr, because you want rows not columns
- Use diag to get the diagonal specifically out of the matrix
How to find correlations of corresponding rows of two arrays?
17 ビュー (過去 30 日間)
古いコメントを表示
I have 2 arrays --- d1 and d2. d1 = [ 3 4 5 6 7 ; 8 9 10 11 12 ; 13 14 15 16 17 ]; and d2 = [ 18 19 20 21 22 ; 23 24 25 26 27 ; 28 29 30 31 32 ]. How do I find the correlation of each corresponding row? For example, what is the correlation of the first row of d1 ( [ 3 4 5 6 7 ] ) to the first row of d2 ( [ 18 19 20 21 22 ] ). Similarly, what are the two coefficients for the other two corresponding rows?
I tried the following code:
[r, pV]=corr(d1,d2,'rows','complete');
But it seems to return the coefficient of each cell with respect to all the cells. In this case, I want only 3 integers returned (each for each corresponding row). Any ideas on achieving this? Appreciate your assistance!
0 件のコメント
採用された回答
Dave B
2021 年 11 月 22 日
編集済み: Dave B
2021 年 11 月 22 日
The correlations of columns are the diagonal of the correlation matrix, so you can
d1 = rand(3,5);
d2 = rand(3,5);
C=corr(d1',d2')
diag(C)
% Check that it's the same result as taking the correlations of each row
% indpendently
[corr(d1(1,:)',d2(1,:)') corr(d1(2,:)',d2(2,:)') corr(d1(3,:)',d2(3,:)')]'
その他の回答 (0 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!