Filter löschen
Filter löschen

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

9 Ansichten (letzte 30 Tage)
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'.

Akzeptierte Antwort

Paul
Paul am 25 Mai 2021
Bearbeitet: Paul am 25 Mai 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

Weitere Antworten (0)

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by