Azzera filtri
Azzera filtri

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

3 visualizzazioni (ultimi 30 giorni)
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!

Risposta accettata

Conrad
Conrad il 13 Lug 2012
Modificato: Conrad il 13 Lug 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

Più risposte (0)

Categorie

Scopri di più su Matrices and Arrays in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by