- 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?
8 views (last 30 days)
Show older comments
Rabeca Mohammed
on 22 Nov 2021
Commented: Rabeca Mohammed
on 23 Nov 2021
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 Comments
Accepted Answer
Dave B
on 22 Nov 2021
Edited: Dave B
on 22 Nov 2021
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,:)')]'
More Answers (0)
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!