سؤال

I have two matrices:

Input:

A = [1,1;2,4;3,9;4,16;5,25];
B = [3,9;4,16;6,26;7,49;5,25];

Output:

A = [1,1;2,4;3,9;4,16;5,25];
B = [6,26;7,49];

I want to delete the repeating elements of A from B or vice-versa. Below is my current approach, but although correct, it is quite slow.

clear all;
clc;

A = [1,1;2,4;3,9;4,16;5,25];
B = [3,9;4,16;6,26;7,49;5,25];

C = B; 
L = 0;
for ii = 1:length(A)
    for jj = 1:length(B)
        if A(ii,1)==B(jj,1) && A(ii,2)==B(jj,2)

            C(jj,1)=0; 
            C(jj,2)=0; 

            L = L+1;
        end       
    end    
end
L
[A B C]


B = zeros(L-1,2);
L = 1;
for ii = 1:length(C)
    if C(ii,1)~=0 && C(ii,2)~=0

        B(L,1) = C(ii,1);
        B(L,2)=  C(ii,2);

        L = L+1;
    end
end
B

Can I do it by using find command? or by using the intersect command ? Thank you all for your help. For an matrix having a single column or a single row I have been able to do it by using the find command or the intersect command.

هل كانت مفيدة؟

المحلول

>> B(~ismember(B, A, 'rows'), :)

ans =

 6    26
 7    49

نصائح أخرى

try intersect and setdiff to achive this goal

However you did it for a single column using intersect(A,B), you should be able to do it for multiple columns using intersect(A,B,'rows')

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top