yankeefan11
yankeefan11

Reputation: 485

Sorting by imaginary part in MatLab

So I have a vector that contains complex values. I want to sort them in order of ascending imaginary order. Is there a way to do this?

Accorsing to the sort documentation, sort will sort via magnitude.

Thanks

Upvotes: 3

Views: 2911

Answers (1)

Nick
Nick

Reputation: 3193

Please try something like this:

a = [1 + 1i; 1 - 1i; 1 - 2*1i];
[sorted, idx] = sort(imag(a));
a = a(idx);

a = 

1.0000 - 2.0000i
1.0000 - 1.0000i
1.0000 + 1.0000i

Upvotes: 8

Related Questions