In a cell with 255x1 entries, find the intersection of each entry list with all other entry lists
2 ビュー (過去 30 日間)
古いコメントを表示
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!
0 件のコメント
採用された回答
Conrad
2012 年 7 月 13 日
編集済み: Conrad
2012 年 7 月 13 日
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
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Matrices and Arrays についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!