How to Sort a Table by Columns
45 ビュー (過去 30 日間)
古いコメントを表示
MathWorks Support Team
2017 年 5 月 1 日
編集済み: MathWorks Support Team
2017 年 5 月 15 日
I have a table with column names that I would like in alphabetical order. Additionally, I need to keep the first column in its place. Is there a way to do this?
採用された回答
MathWorks Support Team
2017 年 5 月 15 日
編集済み: MathWorks Support Team
2017 年 5 月 15 日
In MATLAB R2016b, there is a way to sort the variable names (column names) using the sort function. Note that the following example will only work as expected on tables where all variables have variable names defined:
% Load sample data available in MATLAB and create a table
load patients
T = table(Weight, Height, Smoker, Gender);
To exclude the first variable name (column name) from the sorting:
sortedNames = sort(T.Properties.VariableNames(2:end));
T2 = [T(:,1) T(:,sortedNames)]
To include all variable names in the sorting:
T3 = T(:,sort(T.Properties.VariableNames))
0 件のコメント
その他の回答 (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!