How can I sort data like this?
1 view (last 30 days)
Show older comments
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 Comments
Accepted Answer
More Answers (2)
Matt Fig
on 24 Nov 2012
Edited: Matt Fig
on 24 Nov 2012
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)))
Azzi Abdelmalek
on 24 Nov 2012
Edited: Azzi Abdelmalek
on 24 Nov 2012
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
See Also
Categories
Find more on Shifting and Sorting Matrices in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!