Effacer les filtres
Effacer les filtres

order a cell array by the dimension of its cells

10 vues (au cours des 30 derniers jours)
simone clochiatti
simone clochiatti le 1 Sep 2015
Commenté : Sinan Islam le 14 Juin 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.

Réponse acceptée

Cedric
Cedric le 1 Sep 2015
Modifié(e) : Cedric le 1 Sep 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 commentaires
Cedric
Cedric le 2 Sep 2015
My pleasure.
Sinan Islam
Sinan Islam le 14 Juin 2022
@Cedric Wannaz Thank you!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Shifting and Sorting Matrices dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by