Search code examples
matlabcell-array

How to remove all vectors from a cell B which belong to A or which are contained in one vector from A?


A = {[1 2 3 4],[22 55 78 84],[50 21 98 71],[10 15 16]};
B = {[2 4],[20 30 55],[16 15 10],[22 55 78]};

How to remove all vectors from a cell B which belong to A or which are contained in one vector from A?

The desired result for my example

out = {[20 30 55]}

Solution

  • A one-liner:

    out = B(~cellfun(@(y) any(cellfun(@(x) all(ismember(y,x)), A)), B));
    

    The explanation of the code is just saying in other words what you asked for: the inner cellfun detects if a vector of B is completely contained by one of the vectors of A, and the outer cellfun assembles these results for all B vectors. The resulting logical vector (the size of B) is negated, because you want the vectors that are unique to B, not the ones "embedded" in A.