How to Sort a Table by Columns

51 visualizaciones (últimos 30 días)
MathWorks Support Team
MathWorks Support Team el 1 de Mayo de 2017
Editada: MathWorks Support Team el 15 de Mayo de 2017
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?

Respuesta aceptada

MathWorks Support Team
MathWorks Support Team el 15 de Mayo de 2017
Editada: MathWorks Support Team el 15 de Mayo de 2017
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))

Más respuestas (0)

Categorías

Más información sobre Shifting and Sorting Matrices en Help Center y File Exchange.

Etiquetas

Aún no se han introducido etiquetas.

Productos


Versión

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by