Correlate values below diagonal of two matrices

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
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

Thanks the cyclist. Just wanted to double-check something. Suppose each of the my 4x4 matrices had these values:
a1 a2 a3 a4
b1 b2 b3 b4
c1 c2 c3 c4
d1 d2 d3 d4
Is your suggestion going to correlate a1 from A with a1 from B, a2 from A with a2 from B, a3 from A with a3 from B, a4 from A with a4 from B, etc (this is what I would like)?
Yes, except it will choose from the below the diagonal, not above.

Connectez-vous pour commenter.

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by