How to find unique cells (with numbers NOT string) among cell array in Matlab

Tag: arrays , matlab , integer , unique , cell Author: hebutfls Date: 2013-05-14

I would like to ask how to find unique cells (with integer number NOT string) from cell array like this (size of A is always m x m):

A=({1,3,4} {4,7} {1,3,4};
    {3,6}  {4,7} {};
    {1,3,4}  {4,7} {4});

The results which I want to obtain is:

uniqueA = {1,3,4} {4,7} {3,6} {4}

Do you have any idea?

Best Regards Karolina

Best Answer

you can convert the cell to string fromat:

B = cellfun(@(x)(mat2str(x)),A,'uniformoutput',false);

Then use unique as usual:

[C,ia] = unique(B)

then use the index ia to point to unique cells with:

A{ia}

Other Answer1

If you write A as follow :

A={[1,3,4] [4,7] [1,3,4]; [3,6]  [4,7] []; [1,3,4]  [4,7] [4]};
tmp = cellfun(@(x)(num2str(x)),A,'uniformoutput',false);
unique(tmp);