MATLAB: How to sort an array in descending order

arraydescending ordersortsort an array

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

Best Answer

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.