1 view (last 30 days)

I have a matrix that is 122x4. These have index numbers 1-36 with two only rows containing three points that are common between them. How can I find these pairs?

ex

A=[ 1 2 13 15;

1 2 15 35;

1 2 17 22;

1 2 22 36...]

(there is not a pattern throughout the matrix like there are in the first couple rows) I want some thing that can identify the pair of combos

combos = [1 2;

3 4]

where 1,2,3,and 4 are row numbers

If I can also print out the numbers they share that would be great too combos = [1 2 1 2 15;...etc

Guillaume
on 17 Oct 2017

Edited: Guillaume
on 17 Oct 2017

This is O(n^2) so is going to be slow for largish matrices. I haven't got the time to think of a cleverer algorithm:

pairs = zeros(0, 5);

for row1 = 1 : size(A, 1)

for row2 = row1+1 : size(A, 1)

commonnumbers = intersect(A(row1, :), A(row2, :));

if numel(commonnumbers ) == 3

pairs = [pairs; row1, row2, commonnumbers]; %#ok<AGROW>

break; %assumes that there is always only one other row that matches

end

end

end

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 7 Comments

## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/361257-how-to-find-set-of-3-in-a-122x4-matrix#comment_492938

⋮## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/361257-how-to-find-set-of-3-in-a-122x4-matrix#comment_492938

## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/361257-how-to-find-set-of-3-in-a-122x4-matrix#comment_492947

⋮## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/361257-how-to-find-set-of-3-in-a-122x4-matrix#comment_492947

## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/361257-how-to-find-set-of-3-in-a-122x4-matrix#comment_494029

⋮## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/361257-how-to-find-set-of-3-in-a-122x4-matrix#comment_494029

## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/361257-how-to-find-set-of-3-in-a-122x4-matrix#comment_494038

⋮## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/361257-how-to-find-set-of-3-in-a-122x4-matrix#comment_494038

## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/361257-how-to-find-set-of-3-in-a-122x4-matrix#comment_494043

⋮## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/361257-how-to-find-set-of-3-in-a-122x4-matrix#comment_494043

## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/361257-how-to-find-set-of-3-in-a-122x4-matrix#comment_494059

⋮## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/361257-how-to-find-set-of-3-in-a-122x4-matrix#comment_494059

## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/361257-how-to-find-set-of-3-in-a-122x4-matrix#comment_494062

⋮## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/361257-how-to-find-set-of-3-in-a-122x4-matrix#comment_494062

Sign in to comment.