Search code examples
arraysmatlabmatrixduplicatescell-array

how to remove duplicate matrixes in a cell array?


I have a cell matrix TILER that include some matrix with different sizes.

TILER=[22x4 double]    [1265x4 double]    [58x4 double]    [31x4 double]     [58x4 double]

and we know that some matrixes are duplicate, for example [58x4 double] matrix. how can we remove duplicate matrixes from a cell array to have :

TILER=[22x4 double]    [1265x4 double]    [58x4 double]    [31x4 double]

here is a link, but it doesn't work


Solution

  • In this case it's hard to avoid loops. You can go along the following lines:

    ind = true(1,numel(TILER)); %// true indicates non-duplicate. Initiallization
    for ii = 1:numel(TILER)-1
        for jj = ii+1:numel(TILER)
            if isequal(TILER{ii}, TILER{jj})
                ind(jj) = false; %// mark as duplicate
            end
        end
    end
    TILER2 = TILER(ind);
    

    Some optimizations are possible. For example:

    • In the inner loop don't consider values that have been detected as duplicates in a previous iteration of the outer loop.
    • Do a first check for equality of matrix sizes (this can be done efficiently with bsxfun), and then only test for equality between matrices that have the same size.