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