Sorting a Cell Array
138 ビュー (過去 30 日間)
古いコメントを表示
Hi,
Sorry if this has been asked / answered many times or very simple etc. etc!
If I have a cell array say Array = {B C A;1 3 2; D E F}
Is it possible to sort the first row in order, so its A B C, with the second and third rows changing corresponding to to this initial sort?
Final answer being {A B C;2 1 3;F D E}
Thanks in advance Matthew
0 件のコメント
採用された回答
Andrei Bobrov
2011 年 12 月 23 日
Array = {'B' 'C' 'A';1 3 2; 'D' 'E' 'F'}
out = sortrows(Array',1)';
OR
[id id] = sort(Array(1,:))
out = Array(:,id)
3 件のコメント
NALLARASU KRISH
2022 年 3 月 10 日
What is the syntax for sorting cell arrays in descending order? Thank You!
Ananya
2024 年 4 月 26 日
sortrows(Array',1)':: here since sortrows works for columns ,(sorting elements in a column of a matrix...)
you needed the row to be sorted here , so Array' is the transpose of it and then doing a retranspose of the result by doing sortrows(Array',1)'<-- to get the original matrix
for descending its simple, just add 'descend' at the end of these arguments (Array',1,'descend')'
その他の回答 (3 件)
Andreas Goser
2011 年 12 月 23 日
It is not clear to me what your cell array really looks like, as your example is not valid, but the SORTROWS command should work.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Shifting and Sorting Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!