How to Sort a Table by Columns
조회 수: 49 (최근 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!