필터 지우기
필터 지우기

sort a matrix in a specific way

조회 수: 2 (최근 30일)
Homayoon
Homayoon 2015년 6월 22일
편집: Stephen23 2015년 6월 22일
Dear All, Although I know how to sort a single column or row in an ascending or descending way, but I do not know how to sort a higher dimension matrix based on a single column. I hope the following example illustrate my point:
12 0 1 65
5 1 0 340
13 2 1 100
9 0 0 0
I need the matrix to be sorted based on the first column. Basically I need all rows to be sorted based on a simple sort on the first column. Here is the desired output:
5 1 0 340
9 0 0 0
12 0 1 65
13 2 1 100
I hope you will help me to figure how to sort it out! Thank you so much HRJ

채택된 답변

Stephen23
Stephen23 2015년 6월 22일
편집: Stephen23 2015년 6월 22일
You can use sortrows for this:
>> A = [12,0,1,65; 5,1,0,340; 13,2,1,100; 9,0,0,0]
A =
12 0 1 65
5 1 0 340
13 2 1 100
9 0 0 0
>> sortrows(A,1)
ans =
5 1 0 340
9 0 0 0
12 0 1 65
13 2 1 100
  댓글 수: 2
Homayoon
Homayoon 2015년 6월 22일
Thanks. I was not aware of this syntax I do appreciate your help
Stephen23
Stephen23 2015년 6월 22일
편집: Stephen23 2015년 6월 22일
My pleasure. You can also Accept an answer that resolves your question.

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

추가 답변 (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