Filter löschen
Filter löschen

order a cell array by the dimension of its cells

10 Ansichten (letzte 30 Tage)
simone clochiatti
simone clochiatti am 1 Sep. 2015
Kommentiert: Sinan Islam am 14 Jun. 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.

Akzeptierte Antwort

Cedric
Cedric am 1 Sep. 2015
Bearbeitet: Cedric am 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 Kommentare
Cedric
Cedric am 2 Sep. 2015
My pleasure.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Shifting and Sorting Matrices finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by