Sort every single row

조회 수: 6 (최근 30일)
Patrick
Patrick 2015년 2월 18일
편집: Stephen23 2015년 2월 18일
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!

채택된 답변

per isakson
per isakson 2015년 2월 18일
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
Patrick
Patrick 2015년 2월 18일
Thank you man!
Stephen23
Stephen23 2015년 2월 18일
편집: Stephen23 2015년 2월 18일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by