简体   繁体   English

查找矩阵中不相交的行

[英]Finding the non-intersecting rows in a matrix

I want to find the non-intersecting rows in a large matrix. 我想在一个大矩阵中找到不相交的行。 As an example: 举个例子:

A=[1 5 3; 3 4 5; 7 9 10;4 5 6;11 2 8; 3 5 10]

In this matrix, the non-intersecting rows are: [1 5 3], [11 2 8] and [7 9 10] . 在此矩阵中,不相交的行是: [1 5 3], [11 2 8][7 9 10] How can I program this in Matlab in a fast way? 如何快速在Matlab中对此编程?

If I may bsxfun - 如果我可以bsxfun

M = squeeze(any(bsxfun(@eq,A,permute(unique(A),[3 2 1])),2))
[~,row_idx] = max(M,[],1)
out = A(sum(M,2).' == histc(row_idx,1:size(A,1)),:)

Sample step-by-step run - 逐步执行范例-

A =
     1     5     3
     3     4     5
     7     9    10
     4     5     6
    11     2     8
     3     5    10
M =
     1     0     1     0     1     0     0     0     0     0     0
     0     0     1     1     1     0     0     0     0     0     0
     0     0     0     0     0     0     1     0     1     1     0
     0     0     0     1     1     1     0     0     0     0     0
     0     1     0     0     0     0     0     1     0     0     1
     0     0     1     0     1     0     0     0     0     1     0
row_idx =
     1     5     1     2     1     4     3     5     3     3     5
out =
     1     5     3
     7     9    10
    11     2     8

You can look for rows that adding them to union of previous rows increases the number of elements in the union by the number of columns (ie all elements in that row are new): 您可以查找将它们添加到先前行的并集中的行,从而增加并列中元素的数量(即该行中的所有元素都是新的):

B = []; 
C = zeros(1,size(A,1)); 
for k=1:size(A,1), 
    B1 = union(B, A(k,:)); 
    C(k) = numel(B1)-numel(B); 
    B=B1; 
end
result = A(C==size(A,2),:);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM