How to find correlations of corresponding rows of two arrays?

3 次查看(过去 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!

采纳的回答

Dave B
Dave B 2021-11-22
编辑:Dave B 2021-11-22
The correlations of columns are the diagonal of the correlation matrix, so you can
  • 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
d1 = rand(3,5);
d2 = rand(3,5);
C=corr(d1',d2')
C = 3×3
-0.2127 0.6956 0.0173 -0.6065 0.9023 0.4460 0.1537 -0.4428 -0.0161
diag(C)
ans = 3×1
-0.2127 0.9023 -0.0161
% 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,:)')]'
ans = 3×1
-0.2127 0.9023 -0.0161

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by