Borrar filtros
Borrar filtros

Sorting cell arrays with characters and numbers

8 visualizaciones (últimos 30 días)
Araya Mengistu
Araya Mengistu el 30 de Oct. de 2022
Comentada: Thomas Jarry el 5 de Nov. de 2022
Given a cell array such as {'a1', 'a2', 'a3'...'a9', 'a10', 'a11','a12'}, in random order ofcourse, using the sort function gives me {'a1', 'a10', 'a11','a12','a1','a2',...'a9'}. What method can be used to get the right order?

Respuesta aceptada

Stephen23
Stephen23 el 30 de Oct. de 2022
Editada: Stephen23 el 30 de Oct. de 2022
"What method can be used to get the right order?"
You could download my FEX submission NATSORT:
unzip it onto your MATLAB search path or into the current directory, then call it like this:
C = {'a11', 'a9', 'a3', 'a2', 'a12', 'a1','a10'}
C = 1×7 cell array
{'a11'} {'a9'} {'a3'} {'a2'} {'a12'} {'a1'} {'a10'}
D = natsort(C)
D = 1×7 cell array
{'a1'} {'a2'} {'a3'} {'a9'} {'a10'} {'a11'} {'a12'}
  2 comentarios
Araya Mengistu
Araya Mengistu el 31 de Oct. de 2022
Worked great. Appreciated.
Thomas Jarry
Thomas Jarry el 5 de Nov. de 2022
23 thanks Stephen ! :-)

Iniciar sesión para comentar.

Más respuestas (1)

Walter Roberson
Walter Roberson el 30 de Oct. de 2022
Editada: Walter Roberson el 30 de Oct. de 2022
MATLAB already gives the "right" order, just not the order you want.
https://www.mathworks.com/matlabcentral/fileexchange/34464-customizable-natural-order-sort

Categorías

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

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by