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

9 次查看(过去 30 天)
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'.

采纳的回答

Paul
Paul 2021-5-25
编辑:Paul 2021-5-25
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by