Sorting complex array with cplxpair and how to get the sort index?

7 views (last 30 days)
I have a vector of complex numbers, which I sort using cplxpair to get the complex conjugates in ordered pair. But now I need to know the index with respect to the orginal vector.
For example,
I have,
k = [
0.0000 - 0.9998i
0.0000 - 0.9995i
0.0000 + 0.9998i
0.0000 + 0.9995i
-0.0001 - 0.9491i
0.0000 + 0.9995i
0.0000 - 0.9995i
-0.0001 + 0.9491i
]
I use cplxpair(k) and I get,
kp = [
-0.0001 - 0.9491i
-0.0001 + 0.9491i
0.0000 - 0.9998i
0.0000 + 0.9998i
0.0000 - 0.9995i
0.0000 + 0.9995i
0.0000 - 0.9995i
0.0000 + 0.9995i
]
But I need to know how to get the index of 'kp' relating to that of 'k'.

Accepted Answer

Paul
Paul on 25 May 2021
Edited: Paul on 25 May 2021
ismember() might be what you're looking for. Do you care about how this works if the elements of k are not all unique?
k = [
0.0000 - 0.9998*1i
0.0000 - 0.9995*1i
0.0000 + 0.9998*1i
0.0000 + 0.9995*1i
-0.0001 - 0.9491*1i
0.0000 + 0.9995*1i
0.0000 - 0.9995*1i
-0.0001 + 0.9491*1i];
kp = cplxpair(k);
[~,ind] = ismember(k,kp);
[kp(ind) k]
ans =
0.0000 - 0.9998i 0.0000 - 0.9998i 0.0000 - 0.9995i 0.0000 - 0.9995i 0.0000 + 0.9998i 0.0000 + 0.9998i 0.0000 + 0.9995i 0.0000 + 0.9995i -0.0001 - 0.9491i -0.0001 - 0.9491i 0.0000 + 0.9995i 0.0000 + 0.9995i 0.0000 - 0.9995i 0.0000 - 0.9995i -0.0001 + 0.9491i -0.0001 + 0.9491i
isequal(kp(ind),k)
ans = logical
1
Note that k contained duplicate values, so ind is not just a shuffle of 1:8
sort(ind).'
ans = 1×8
1 2 3 4 5 5 6 6

More Answers (0)

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!