In a cell with 255x1 entries, find the intersection of each entry list with all other entry lists

1 view (last 30 days)
I have a cell that is 255x1, and cell each entry contains a matrix that is nx3. I need to have each matrix in the cell compare with all other matrices to find the intersections. Order of the three columns in the row of each matrix does not matter. If my thinking is correct, Matrix 1 will compare with Matrices 2-255, and so on, all the way to Matrix 254 comparing with Matrix 255 (255 choose 2 = 32385 comparisons, if the logic is correct).
I would like each comparison to be placed in a matrix in a separate cell, with the number of the two matrices being compared appearing in the cell (for example, if the intersection of Matrix 4 and Matrix 87 is being found, 4 and 87 will appear as the first two rows). Only the intersecting rows will appear in the final product.
Thank you for your help!

Accepted Answer

Conrad
Conrad on 13 Jul 2012
Edited: Conrad on 13 Jul 2012
Hi Rebecca, you could try this:
C = {
[ 1 2 3;
4 5 6;
2 3 1];
[ 1 3 3;
2 3 1;
4 5 7];
[ 3 3 3;
3 2 1;
4 5 5]
};
P = nchoosek(1:size(C,1),2);
R = cellfun(@(m1,m2)intersect(sort(m1,2),sort(m2,2),'rows'),...
C(P(:,1)),C(P(:,2)),'UniformOutput',false);
idx = P(cell2mat(cellfun(@(x)size(x,1),R,'UniformOutput',false))==1,:);
idx will then contain the indices of the matrices that delivers that results in an intersection. R contains the intersected rows.
Conrad

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!