Borrar filtros
Borrar filtros

order a cell array by the dimension of its cells

10 visualizaciones (últimos 30 días)
simone clochiatti
simone clochiatti el 1 de Sept. de 2015
Comentada: Sinan Islam el 14 de Jun. de 2022
Hi, I am looking for a simple and possibly clean way to sort the cells of a cell array by the length of the vector inside every cell. For instance I have this cell array:
c =
[1x3 double]
[1x4 double]
[1x2 double]
[1x3 double]
and i want to obtain this:
c =
[1x2 double]
[1x3 double]
[1x3 double]
[1x4 double]
As I said I was looking for something clean rather than fast, and I need working it just for array cells and not for matrix cells in general, thank you very much in advance.

Respuesta aceptada

Cedric
Cedric el 1 de Sept. de 2015
Editada: Cedric el 1 de Sept. de 2015
Here is one way:
Build test data:
>> c = {randi(10,1,3), randi(10,1,4),randi(10,1,2),randi(10,1,3)}'
c =
[1x3 double]
[1x4 double]
[1x2 double]
[1x3 double]
Sort cells' content length, get indices of ordered lengths:
>> [~,id] = sort( cellfun( @length, c ))
id =
3
1
4
2
Use indices to sort original cell array:
>> c = c(id)
c =
[1x2 double]
[1x3 double]
[1x3 double]
[1x4 double]
  3 comentarios
Cedric
Cedric el 2 de Sept. de 2015
My pleasure.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Shifting and Sorting Matrices 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