Sort a table with different orders for different columns
18 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Karol Buchajczuk
el 31 de Mzo. de 2021
Comentada: Karol Buchajczuk
el 31 de Mzo. de 2021
Assume table A:
A = [
'k' 2 4
'a' 3 8
'a' 4 5
'k' 2 2
'a' 3 16
'k' 9 3
'k' 9 8
'a' 4 6
]
If i use sortrows(A) every column is sorted with ascending order:
A = [
'a' 3 8
'a' 3 16
'a' 4 5
'a' 4 6
'k' 2 2
'k' 2 4
'k' 9 3
'k' 9 8
]
Is there a way to sort rows, so first and second column is sorted with ascending order, but third one with descending? I want to get something like this:
A = [
'a' 3 16
'a' 3 8
'a' 4 6
'a' 4 5
'k' 2 4
'k' 2 2
'k' 9 8
'k' 9 3
]
3 comentarios
Star Strider
el 31 de Mzo. de 2021
It would likely be necessary to sort each column independently, then concatenate the results into a new matrix.
Respuesta aceptada
Más respuestas (1)
David Hill
el 31 de Mzo. de 2021
You can't have a matrix with characters and numbers. But if you convert the characters to double, then:
A=[sort(A(:,1)),sort(A(:,2)),sort(A(:,3),'descend')];
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!