필터 지우기
필터 지우기

How to sort an array in descending order?

조회 수: 66 (최근 30일)
vatankhah
vatankhah 2013년 9월 8일
댓글: Image Analyst 2017년 12월 7일
For example with an array like X that is:
X=[10 3 6 8;
12 5 9 2;
66 4 7 11];
I want a code that gives me the total sort of array X in descending order:
sorted_X=[66 12 11 10;
9 8 7 6;
5 4 3 2];
thanks

채택된 답변

Image Analyst
Image Analyst 2013년 9월 8일
Try this:
[rows, columns] = size(X)
sorted_X = reshape(sort(X(:), 'descend'), [columns, rows])'
It gives you the exact result you showed. It sorts all the elements, regardless of what row or column they started in, then reshapes from a vector back into the shape of the original X matrix.
  댓글 수: 1
Mr. Suraj
Mr. Suraj 2016년 12월 26일
Very Educative indeed, helpful and time saving, Thanks

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

추가 답변 (3개)

Markus
Markus 2013년 9월 8일

Walter Roberson
Walter Roberson 2013년 9월 8일
Do the total sort in ascending order, and then reverse the order of the elements.

venkatesh dixit
venkatesh dixit 2017년 12월 7일
편집: Image Analyst 2017년 12월 7일
I have a matrix, and I have to keep the columns in ascending order of the sum of the matrix, or descending order or sum of matrix. Kindly tell how to do it. My matrix is as follows:
A=[1 3 3;
4 5 6;
9 18 17]
I have to sort it accordingly so that it becomes
[3 3 1;
6 5 4;
17 18 9];
  댓글 수: 1
Image Analyst
Image Analyst 2017년 12월 7일
Not clear at all. Are you sorting the first row somehow and applying the sort order to the other rows? If so, it's ambiguous because you have 3 in two locations. Like this:
[~, sortOrder] = sort(A(1,:), 'descend')
out = A(:, sortOrder)
Or are you summing horizontally to get the sum along each row, or vertically to get the sum along each column? It's just not clear at all. Please try to explain it more accurately.

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

카테고리

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