Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 255
Karolina Avatar asked Dec 06 '25 14:12

Karolina


2 Answers

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}
like image 102
bla Avatar answered Dec 08 '25 06:12

bla


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);
like image 38
m_power Avatar answered Dec 08 '25 07:12

m_power



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!