Asked by Paolo Bighignoli
on 14 Jun 2018 at 10:48

I have a vector of real and complex number, and without using a routine i was wondering if there is a comand to find the numbers with smallest imaginary part and their index in the vector. So for example if the vector is v = [1 2 3 4+5i 6+7i 8 9+10i] the output that i want (if i want for example the 2 smallest) is

b = [4+5i 6+7i] and i = [4 5].

Answer by Adam
on 14 Jun 2018 at 11:06

Edited by Adam
on 14 Jun 2018 at 11:07

Accepted Answer

Something like this would do it, albeit with some better variable naming and handling of error cases.

[~, idx] = sort( nonzeros( imag( v ) ) ); imagIdx = find( imag( v ) ~= 0 );

n = 2; % The number of smallest to extract i= imagIdx( idx(1:n) );

b = v( :, i);

Paolo Bighignoli
on 14 Jun 2018 at 14:30

Thank you Adam it's exactly what i was searching for.

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 2 Comments

## Guillaume (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/405658-find-smallest-imaginary-values-of-a-vector-of-real-and-complex-number#comment_578667

Aren't 2, 3, and 8 the complex numbers with the smallest imaginary part (equal to 0)?

## Paolo Bighignoli (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/405658-find-smallest-imaginary-values-of-a-vector-of-real-and-complex-number#comment_578749

Hi Guillaume, yes they are the ones with the smallest part but sorry if i didn't explained exactly. I'm searching for the smallest but also >0.

Sign in to comment.