Find smallest imaginary values of a vector of real and complex number
3 views (last 30 days)
Show older comments
Paolo Bighignoli
on 14 Jun 2018
Commented: Paolo Bighignoli
on 14 Jun 2018
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].
2 Comments
Guillaume
on 14 Jun 2018
Aren't 2, 3, and 8 the complex numbers with the smallest imaginary part (equal to 0)?
Accepted Answer
Adam
on 14 Jun 2018
Edited: Adam
on 14 Jun 2018
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);
More Answers (1)
KSSV
on 14 Jun 2018
v = [1 2 3 4+5i 6+7i 8 9+10i] ;
[val,idx] = sort(imag(v)) ;
iwant = v(idx(val~=0))
0 Comments
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!