sorting in descending order by row

4 visualizaciones (últimos 30 días)
arad rad
arad rad el 21 de Ag. de 2018
Comentada: Stephen23 el 22 de Ag. de 2018
hi, i have a square matrix and i need to sort it in descending order in each row, i mean from left to right along columns for each row. all the functions like sort and sortrow are in ascending order or sort in descending order by column ! thank you
  2 comentarios
Stephan
Stephan el 21 de Ag. de 2018
If you need a good performance, then accept the answer given by Stephen Cobeldick:
A = rand(12000) - 0.5;
tic
B = sort(A,2,'descend'); % The way i did it
toc
tic
C = -sort(-A.').'; `% the way Stephen Cobeldick did
toc
with result:
>> sort_performance
Elapsed time is 3.725967 seconds.
Elapsed time is 2.971930 seconds.
and the winner is... ;-)
arad rad
arad rad el 22 de Ag. de 2018
thanks it was really helpful

Iniciar sesión para comentar.

Respuesta aceptada

Stephen23
Stephen23 el 21 de Ag. de 2018
Where M is your matrix:
>> M = randi(9,4)
M =
6 1 9 5
5 1 8 8
6 4 9 9
6 3 3 4
>> -sort(-M.').'
ans =
9 6 5 1
8 8 5 1
9 9 6 4
6 4 3 3
  2 comentarios
arad rad
arad rad el 22 de Ag. de 2018
thank u
Stephen23
Stephen23 el 22 de Ag. de 2018
@arad rad: you should really use Stephan Jungs's answer.

Iniciar sesión para comentar.

Más respuestas (1)

Stephan
Stephan el 21 de Ag. de 2018
Hi,
use:
A =
10 -12 4 8
6 -9 8 0
2 3 11 -2
1 1 9 3
>> sort(A,2,'descend')
ans =
10 8 4 -12
8 6 0 -9
11 3 2 -2
9 3 1 1
to sort descending in row-direction.

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