Search code examples
matlabcellintersection

Intersection of two cell arrays of strings in MATLAB


I have two cell arrays, X and Y. Each cell array is made up of strings : i.e, X{i} is a string for all i, and so is Y{i}. I would like to find the intersection of the two cell arrays, (presumably a third cell array} that has the strings common to both X and Y.


Solution

  • There might a single function that does this - I don't remember. But you can do it pretty easily with ismember:

    a = {'a', 'b', 'c'};
    b = {'b', 'd', 'a'};
    intersection = a(ismember(a, b));