Viktor
Viktor

Reputation: 141

Matlab - sort by absolute value

How do I sort a vector in matlab by absolute value?

Upvotes: 5

Views: 6127

Answers (1)

Jonas
Jonas

Reputation: 74940

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

Upvotes: 9

Related Questions