フィルターのクリア

order a cell array by the dimension of its cells

10 ビュー (過去 30 日間)
simone clochiatti
simone clochiatti 2015 年 9 月 1 日
コメント済み: Sinan Islam 2022 年 6 月 14 日
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.

採用された回答

Cedric
Cedric 2015 年 9 月 1 日
編集済み: Cedric 2015 年 9 月 1 日
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 件のコメント
Cedric
Cedric 2015 年 9 月 2 日
My pleasure.
Sinan Islam
Sinan Islam 2022 年 6 月 14 日
@Cedric Wannaz Thank you!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeShifting and Sorting Matrices についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by