MATLAB: Pair the index of aray’s same values

index of pairsMATLABpairs

Hi Everyone,
I have the following array:
b=[1 2 2 1 3 4 1 3 4 3 5 6]
and I want to make from it this matrix:
pairs=[1 4;
1 7;
2 3;
4 7;
5 8;
5 10;
8 10;
6 9]
It means that the 1st and 4th value of "b" is equal. The 1st and 7th value of "b" is equal too. The 2nd and the 3rd value of "b" is equal. And so on.
Is it understanable?
So, the question is, how can I make the "pairs" matrix from the "b" array?
Thank you!

Best Answer

Try this
b=[1 2 2 1 3 4 1 3 4 3 5 6];
[r, c] = find(squareform(pdist(b.'))==0);
pairs = sortrows([r(c > r) c(c > r)]);
Result
>> pairs
pairs =
1 4
1 7
2 3
4 7
5 8
5 10
6 9
8 10