sort matrix by absolute value

25 visualizaciones (últimos 30 días)
Poorva
Poorva el 29 de Mzo. de 2021
Comentada: Poorva el 30 de Mzo. de 2021
How do I sort a matrix by absolute value and display its original value with +,- sign?

Respuesta aceptada

DGM
DGM el 29 de Mzo. de 2021
Editada: DGM el 29 de Mzo. de 2021
Consider the case for a vector:
A=randi(19,[10 1])-10
[~,idx]=sort(abs(A))
Asorted=A(idx)
For other arrays, you'll have to decide how exactly you want to sort it, and do the indexing accordingly.
  4 comentarios
Poorva
Poorva el 30 de Mzo. de 2021
Thank you ....I got it now and the code is working well.
Poorva
Poorva el 30 de Mzo. de 2021
[y, idx2] = sort(D,2,'descend');
sx = size(C);
index = sub2ind(sx, repmat((1:sx(1)).', 1, sx(2)), idx2);
y2 = C(index);
res=array2table(y2)

Iniciar sesión para comentar.

Más respuestas (1)

Steven Lord
Steven Lord el 30 de Mzo. de 2021
Specify the 'ComparisonMethod' name-value pair argument.
rng default
A = randi([-10 10], 5, 5)
A = 5×5
7 -8 -7 -8 3 9 -5 10 -2 -10 -8 1 10 9 7 9 10 0 6 9 3 10 6 10 4
[values, locations] = sort(A(:), 'ComparisonMethod', 'abs');
[values.'; locations.']
ans = 2×25
0 1 -2 3 3 4 -5 6 6 7 7 -7 -8 -8 -8 9 9 9 9 10 10 10 10 10 -10 14 8 17 5 21 25 7 15 19 1 23 11 3 6 16 2 4 18 24 9 10 12 13 20 22
  2 comentarios
DGM
DGM el 30 de Mzo. de 2021
Do you know about when that option was introduced? I'm guessing it was some time around 2018, but release notes are vague and I only know it's not in R2015b.
Poorva
Poorva el 30 de Mzo. de 2021
thank you steven ....my code is working.

Iniciar sesión para comentar.

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