MATLAB Answers

Getting unique elements that form pairs in a Matrix

10 views (last 30 days)
Abhilash on 25 Oct 2017
Edited: Abhilash on 25 Oct 2017
Dear Everyone,
I have an issue.
I have a matrix of pairwise neuronal correlations. Which means if I have 137 neurons, my matrix is 137x137. So element (13,46) is the correlation between the 13th and the 46th neurons. I want to find all such pairs that have a correlation value of for e.g. > 0.5 (I want to test various thresholds). Then I just want to find the neurons that form these pairs. So if for e.g. the pairs are (13,46), (13,56), (46,56), the output needs to be - 13,46,56.
I first get the lower-half of my symmetric correlation matrix, and replace the upper half in this triangular matrix with NaNs. I also replace the diagonal 1s with NaNs.
And then I use [x,y]=find() to get the subscripts of the elements that exceed my threshold. I then use unique() on the x to get the unique neurons that form these pairs.
The correlation matrix has negative corrs and positive corrs. I only ask for let's say very high correlations. So I end up with around 900 pairs. When I ask for the unique neurons that form these pairs, I get 122 out of 137 for corrs > 0.5, but I get 127 for corrs > 0! This is weird right?
Here is my code -
sigCorrMat = tril(SU(iDataset).signalCorrelation); % Get the lower half
sigCorrMat(sigCorrMat==1) = NaN; % Replace diagonal with NaNs
sigCorrMat(sigCorrMat==0) = NaN; % Replace upper half with NaNs
[x,y]=find(sigCorrMat > thr(1) & sigCorrMat < thr(2)); % Find the pair IDs that satisfy the threshold criterion
allUnits = unique(x); % Get the units that form these pairs
I would be very grateful if someone could help me out. Thanks!


Guillaume on 25 Oct 2017
I get 122 out of 137 for corrs > 0.5, but I get 127 for corrs > 0! This is weird right?
Why is it weird? the set of numbers > 0 is guaranteed to have at least as many elements as the set of numbers > 0.5
Note that comparing to 1 to identify diagonal elements and 0 to identify upper half looks risky. What if you have 1s or 0s in the lower half? Safer (and possibly faster) would be to filter the logical array you pass to find:
sigCorrMat = SU(iDataset).signalCorrelation;
iscorr = sigCorrMat > thr(1) & sigCorrMat < thr(2);
[row, col] = find(tril(iscorr, -1)); %tril(x, -1) only keeps the true values below the diagonal
Abhilash on 25 Oct 2017
Hi Guillaume,
Thanks! THat's what I thought too. I guess this is correct then. I was just very surprised that using a threshold of 0.5 gives me only 5 units fewer than with all > 0.
Thanks for the tip with the triangulation!

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!