2

I have array value like this snippet below:

a =  { [1 2 4]; [3 5 6 7]; [1 2 4]; [3 5 6 7]; [8 9]; []};

I am trying Matlab to get array value like this

a = { [1 2 4]; [3 5 6 7];[8 9]};

eddie
  • 1,252
  • 3
  • 15
  • 20

1 Answers1

0

basic solution for finding uniques

unique(cellfun(@(x)(mat2str(x)),a,'uniformoutput',false))

This can be found here actually.


Complicating factors

Technically the empty cell at the end is also unique, perhaps you want to remove it separately like so:

a(cellfun(@isempty,a)) = []

Currently you get strings as output, this can be solved like so:

[~, idx] = unique(cellfun(@(x)(mat2str(x)),a,'uniformoutput',false))
a(idx)

I personally think that this is harder than it should be.


Summary

You can get your desired output like so

a(cellfun(@isempty,a)) = []
[~, idx] = unique(cellfun(@(x)(mat2str(x)),a,'uniformoutput',false))
a(idx)
Community
  • 1
  • 1
Dennis Jaheruddin
  • 21,208
  • 8
  • 66
  • 122