Search code examples
matlabmatrixdiscrete-mathematicsset-difference

Find and Exclude one Matrix from other


I'm working on MATLAB. I have the following matrices

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

B = [
    1 5 2 3
    6 7 8 9
];

I want to find A-B so that the answer should be like,

ans = [
    1 2 3 4
    5 6 7 8
    1 3 6 2
    6 3 1 6
    9 7 4 7
];

Solution

  • Use setdiff with the 'rows' and 'stable' options:

    >> C = setdiff(A,B,'rows','stable')
    C =
         1     2     3     4
         5     6     7     8
         1     3     6     2
         6     3     1     6
         9     7     4     7