Correlate values below diagonal of two matrices
Afficher commentaires plus anciens
Hi there I have two 4x4 matrices that have identical values below and above the diagonal and zeros along the diagonal (they are distance matrices). I would like to correlate values (and make a scatterplot) from Matrix A with values from Matrix B but I only want to include the values below each diagonal (i.e. 5 cell values from matrix A with 5 cell values from matrix B).
Can anyone please help me with this?
Thanks michelle
Réponses (1)
the cyclist
le 6 Déc 2016
Modifié(e) : the cyclist
le 6 Déc 2016
Here's one way:
% Your matrices. (In your case upper and lower are the same, but that is irrelevant to the algorithm.)
% Also, you could get "N" from the size of your matrices, rather than the other-way-round as I did here.
N = 4;
A = rand(N);
B = rand(N);
% Find logical index to the below-diagonal elements.
[ii,jj] = ind2sub([N,N],1:N^2);
isBelowDiagonal = ii > jj;
% Use the logical index to get the below-diagonal elements of your matrices.
belowDiagonalA = A(isBelowDiagonal);
belowDiagonalB = B(isBelowDiagonal);
% Calculate the correlation matrix of these elements
belowDiagonalCorrelation = corrcoef(belowDiagonalA,belowDiagonalB);
2 commentaires
meechellevdm
le 6 Déc 2016
the cyclist
le 6 Déc 2016
Yes, except it will choose from the below the diagonal, not above.
Catégories
En savoir plus sur Operating on Diagonal Matrices dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!