Sort every single row

1 visualización (últimos 30 días)
Patrick
Patrick el 18 de Feb. de 2015
Editada: Stephen23 el 18 de Feb. de 2015
How can I sort every single row of a matrix in ascending order? For example [16 2 3 13; 5 11 10 8] becomes [2 3 13 16;5 8 10 11] Thanks for help!

Respuesta aceptada

per isakson
per isakson el 18 de Feb. de 2015
One way
M = [16 2 3 13; 5 11 10 8] ;
for rr = 1 : size( M, 1 )
M( rr, : ) = sort( M( rr , : ), 'ascend' );
end
  2 comentarios
Patrick
Patrick el 18 de Feb. de 2015
Thank you man!
Stephen23
Stephen23 el 18 de Feb. de 2015
Editada: Stephen23 el 18 de Feb. de 2015
Doing this in a loop is poor MATLAB code. Use sort's optional second dimension argument instead:
>> A = [16 2 3 13; 5 11 10 8]
>> sort(A,2)
ans =
2 3 13 16
5 8 10 11
This is faster, neater and much more robust.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

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

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by