- 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?
20 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Rabeca Mohammed
le 22 Nov 2021
Commenté : Rabeca Mohammed
le 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 commentaires
Réponse acceptée
Dave B
le 22 Nov 2021
Modifié(e) : Dave B
le 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,:)')]'
Plus de réponses (0)
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!