Delete double cell array entries

2 visualizaciones (últimos 30 días)
Antje
Antje el 20 de Mzo. de 2013
How can I delete double entries in a cell array? I don't want to use a loop. For example I have:
CellArray={[1 2 3] [1 2] [3 5 6 7] [1 2] [2 4] }
And as a result I want to get this:
NewCellArray={[1 2 3] [1 2] [3 5 6 7] [2 4] }
Thank you very much and have a nice day!

Respuesta aceptada

Azzi Abdelmalek
Azzi Abdelmalek el 20 de Mzo. de 2013
Editada: Azzi Abdelmalek el 20 de Mzo. de 2013
CellArray={[1 2 3] [1 2] [3 5 6 7] [1 2] [2 4] };
m=max(cellfun(@numel,CellArray));
v=cellfun(@(x) [x inf(1,m-numel(x))],CellArray,'un',0);
[~,ii]=unique(cell2mat(v'),'rows');
CellArray=CellArray(ii)
  1 comentario
Antje
Antje el 21 de Mzo. de 2013
This solution works really fine and fast! Time needed for my loop: 1.24 seconds Time needed with the one line solution from Friedrich below: 0.3 seconds Time needed with your solution: 0.07 seconds! :) Thanks so much!

Iniciar sesión para comentar.

Más respuestas (2)

Friedrich
Friedrich el 20 de Mzo. de 2013
Hi,
one line:
NewCellArray = cellfun(@str2num, unique(cellfun(@num2str, CellArray,'UniformOutput',false)),'UniformOutput',false)
  1 comentario
Antje
Antje el 21 de Mzo. de 2013
This solution works very good and I like that it is only one line. But in my case I prefer the faster solution from above. Thank you!

Iniciar sesión para comentar.


Babak
Babak el 20 de Mzo. de 2013
CellArray={[1 2 3] [1 2] [3 5 6 7] [1 2] [2 4] };
NewCellArray = [CellArray(1:3) CellArray(5)];
  2 comentarios
Azzi Abdelmalek
Azzi Abdelmalek el 20 de Mzo. de 2013
CellArray is just an example, I guess his array is much bigger.
Antje
Antje el 21 de Mzo. de 2013
Yes, that's the problem. My cell array can have thousands of entries.

Iniciar sesión para comentar.

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Productos

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by