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

조회 수: 8 (최근 30일)
Murthy MVVS
Murthy MVVS 2021년 5월 25일
댓글: Murthy MVVS 2021년 5월 25일
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