Sort Matlab table based on pre-defined order
47 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Metin Akyol
el 31 de En. de 2022
Comentada: Metin Akyol
el 31 de En. de 2022
I would like to sort a table based on a column that only contains 4 string values (A, B, C, and d) in an specific order that is not alphabetic.
This is what I am currently using:
sortrows(table_1,{'col1','col2'},{'ascend' 'descend'})
For col2 I would like to use a custom order that is neither ascend nor descend. So for instance the sorting order should be:
B
A
C
D
0 comentarios
Respuesta aceptada
Stephen23
el 31 de En. de 2022
Editada: Stephen23
el 31 de En. de 2022
You could do something like this, where C is that column:
D = {'B';'A';'C';'D'}; % the desired order
C = {'C';'C';'A';'D';'B';'C';'A';'D';'B'} % the column to sort
[X,Y] = ismember(C,D);
[~,Z] = sort(Y);
D = C(Z) % of course you would sort the table e.g. T(Z,:)
Más respuestas (1)
Ver también
Categorías
Más información sobre Shifting and Sorting Matrices en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!