find missing element between two matrix
16 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I have
A=[1 2; 3 6; 3 5; 1 4; 4 6; 2 7; 1 6; 6 5; 2 3; 6 7; 3 4];
B=[1 2; 1 6; 2 3; 3 4; 4 6];
I want to find missing element between A and B
result should be
res=[3 6; 3 5; 1 4; 2 7; 6 5; 6 7]
I used this
res = A(~all(ismember(A,B),2),:);
But it does not give me [3 6; 1 4]
0 commentaires
Réponse acceptée
Stephen23
le 17 Mar 2020
Modifié(e) : Stephen23
le 17 Mar 2020
>> A = [1,2;3,6;3,5;1,4;4,6;2,7;1,6;6,5;2,3;6,7;3,4];
>> B = [1,2;1,6;2,3;3,4;4,6];
>> R = setdiff(A,B,'rows')
R =
1 4
2 7
3 5
3 6
6 5
6 7
To get the same order, use the 'stable' option or this:
>> [~,X] = setdiff(A,B,'rows');
>> X = sort(X);
>> R = A(X,:)
R =
3 6
3 5
1 4
2 7
6 5
6 7
3 commentaires
Stephen23
le 18 Mar 2020
You could sort the rows first, e.g
>> A = [1,2;3,6;3,5;1,4;4,6;2,7;1,6;6,5;2,3;6,7;3,4];
>> B = [2,1;1,6;2,3;4,3;6,4];
>> [~,X] = setdiff(sort(A,2),sort(B,2),'rows','stable');
>> R = A(X,:);
or for earlier versions:
>> [~,X] = setdiff(sort(A,2),sort(B,2),'rows');
>> X = sort(X);
>> R = A(X,:)
R =
3 6
3 5
1 4
2 7
6 5
6 7
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!