Matlab - absolute value sorting

How to sort a vector in matlab by absolute value?

+5
source share
1 answer

Use the second output of SORT to get the order, then sort the initial array:

a = [-2 1 3 -1.1];

[~,idx] = sort(abs(a));

result = a(idx)

result =
            1         -1.1           -2            3
+9
source

All Articles