简体   繁体   中英

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] . How can I program this in Matlab in a fast way?

If I may 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),:);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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