How can I sort data like this?

I have data like this:
A= [6.5048 + 4.7841i
6.5048 - 4.7841i
1.2539 + 3.7939i
1.2539 - 3.7939i
-3.0305 + 3.2324i
-3.1216 + 5.9919i
-3.0305 - 3.2324i
-3.1216 - 5.9919i];
how can I sort to be following:
A= [6.5048 + 4.7841i
6.5048 - 4.7841i
1.2539 + 3.7939i
1.2539 - 3.7939i
-3.0305 + 3.2324i
-3.0305 - 3.2324i
-3.1216 + 5.9919i
-3.1216 - 5.9919i];
where all data are followed directly by the conjugates?
Best regard
Ali

댓글 수: 2

Azzi Abdelmalek
Azzi Abdelmalek 2012년 11월 24일
How are they sorted?
Husni
Husni 2012년 11월 24일
I am sorry it is not clear, they sorted based on this: first data is 6.5048 + 4.7841i, it then will be followed by its conjugate 6.5048 - 4.7841i which automatically becomes second data. the third data is 1.2539 + 3.7939i and will be followed by 1.2539 - 3.7939i (third & fourth data). The fifth data is -3.0305 + 3.2324i.but in the original data, the conjugate of fifth does not follow, therefore its should be reordered. I hope it is clear?

댓글을 달려면 로그인하십시오.

 채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2012년 11월 24일

0 개 추천

sort(A)

댓글 수: 5

But if sort(A) the data will be:
1.2539 - 3.7939i
1.2539 + 3.7939i
-3.0305 - 3.2324i
-3.0305 + 3.2324i
-3.1216 - 5.9919i
-3.1216 + 5.9919i
6.5048 - 4.7841i
6.5048 + 4.7841i
Azzi Abdelmalek
Azzi Abdelmalek 2012년 11월 24일
Is there a problem?
Matt Fig
Matt Fig 2012년 11월 24일
Why did you accept the answer if you are not satisfied??
Husni
Husni 2012년 11월 24일
Sorry, I try to find good way to explain. Imagine that A is complex power. I want to sort this. The top element has energy 6.5048 + 4.7841i and its complex conjugate 6.5048 - 4.7841i. the second element has energy 1.2539 + 3.7939i and its conjugate 1.2539 - 3.7939i. the third element has energy -3.0305 + 3.2324i but the following data is not its conjugate. Its conjugate locates at data number 7. so the 7 th data is moved to 5 th data (since it is the conjugate of -3.0305 + 3.2324i ) and so on. hopefully it is quite clear :-)
many thanks anyway :-)
Matt Fig
Matt Fig 2012년 11월 24일
See my solution below.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Matt Fig
Matt Fig 2012년 11월 24일
편집: Matt Fig 2012년 11월 24일

1 개 추천

You need more than to simply call SORT. The SORT function sorts complex arrays by magnitude, which is a problem for an array like this:
A = [2+3i;
3+2i;
sqrt(13/2)+sqrt(13/2)*i;
2-3i;
3-2i;
sqrt(13/2)-sqrt(13/2)*i]
% Note all(abs(A)==sqrt(13))
For problems like this you need to do something more involved.
[RA,J] = sort(real(A));
% Now we can put them together
newA = complex(RA,imag(A(J)))

댓글 수: 1

Husni
Husni 2012년 11월 24일
Thanks matt, I got idea from your code

댓글을 달려면 로그인하십시오.

Azzi Abdelmalek
Azzi Abdelmalek 2012년 11월 24일
편집: Azzi Abdelmalek 2012년 11월 24일

0 개 추천

try this
B=A
while length(A)>0;
idx=find(A==conjug(A(1)));
B(ii)=A(1);
B(ii+1)=A(idx);
A(idx)=[];
A(1)=[];
ii=ii+2;
end

카테고리

도움말 센터File Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

제품

태그

질문:

2012년 11월 24일

Community Treasure Hunt

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

Start Hunting!

Translated by