Sorting the complex array elements with imaginary part along with it in MATLAB

不问归期 提交于 2019-12-12 04:47:09

问题


I have a set of data points, which are complex numbers which have, let’s say, 8 rows and 6 columns. If you look at the program, you can see that data sets are arranged very randomly. 1st, 2nd, 3rd columns are okay, but 4th column, is bit anomalous. What I mean to say is that after element at B(5, 4) (i.e. 25.9868674011374) it is coming 2.74257567017122 [B(6, 4)] instead of 26.8410063269595 which has gone to B(6, 6). I want that column arranged in ascending order (every sorting should be done for real part). Also, in the 5th column, even though the real parts ae arranged in ascending order, the imaginary part are kind of swapped. For example, after B(2,5) it should be B(3,6) not B(3,5). I have tried ‘sort’ command, but it not working as I want.

For the people who is wondering wht is the problem with sort, just try to run this command:

A = [1+2i 3+i i 0 -i]; Dreal= sort(real(A)); Dimg = sort(imag(A));D=[Dreal;Dimg];

What I want is -i, 0, i ,3+I,1+2i, what sort gives is something else.

Also, there may be instances where 4th, 5th and 6th column may behave ‘normally’, but 1st, 2nd, 3rd columns behaving erratically. So even if 4th, 5th and 6th columns are behaving ‘normally’ I want it to be sorted down in ascending order. Any way through this? By the by, I am using Matlab- 2015b. The code is:

clear all; clc;
B=[-2.14981736484179 + 0.00000000000000i,-1.38134547606946 + 0.00000000000000i,1.38451324569297 + 0.00000000000000i,22.5759136576435 + 0.00000000000000i,2.28536796878740 + 0.333911501246080i,2.28536796878740 - 0.333911501246080i;-2.22047322414157 + 0.00000000000000i,-1.43596350944258 + 0.00000000000000i,1.43889226552228 + 0.00000000000000i,23.4278498788255 + 0.00000000000000i,2.39484729461819 + 0.303429715954385i,2.39484729461819 - 0.303429715954385i;-2.29148887606605 + 0.00000000000000i,-1.49057388951113 + 0.00000000000000i,1.49328382360683 + 0.00000000000000i,24.2803021611395 + 0.00000000000000i,2.50423839041542 - 0.265225265037282i,2.50423839041542 + 0.265225265037282i;-2.36284412024645 + 0.00000000000000i,-1.54517861014711 + 0.00000000000000i,1.54768832224205 + 0.00000000000000i,25.1333019698605 + 0.00000000000000i,2.61351621914550 + 0.215386193278572i,2.61351621914550 - 0.215386193278572i;-2.43451884781340 + 0.00000000000000i,-1.59977935450756 + 0.00000000000000i,1.60210590260749 + 0.00000000000000i,25.9868674011374 + 0.00000000000000i,2.72266244928806 - 0.142395604889199i,2.72266244928806 + 0.142395604889199i;-2.50649346218904 + 0.00000000000000i,-1.65437753673930 + 0.00000000000000i,1.65653651325774 + 0.00000000000000i,2.74257567017122 + 0.00000000000000i,2.92075248853987 + 0.00000000000000i,26.8410063269595 + 0.00000000000000i;-2.57874914363635 + 0.00000000000000i,-1.70897433922564 + 0.00000000000000i,1.71097996266870 + 0.00000000000000i,2.74509085049068 + 0.00000000000000i,3.13593380433481 + 0.00000000000000i,27.6957188653678 + 0.00000000000000i;-2.65126800477853 + 0.00000000000000i,-1.76357074549849 + 0.00000000000000i,1.76543595943333 + 0.00000000000000i,2.78382099280761 + 0.00000000000000i,3.31458248082037 + 0.00000000000000i,28.5509993172157 + 0.00000000000000i];
k=1:1:4;
B1=sort(real(B));
B2=sort(imag(B));
B3=sort(B);

Thanks in advance.


回答1:


You seem to be wanting the sort(A,'ComparisonMethod','real') feature available in Matlab r2017a, but for use in r2015b.

You can recreate the feature using the sortrows function:

clear
A = [1+2i 3+1i 1i 0 -1i];
AB = [real(A); imag(A)];
[~,I] = sortrows(AB');
Asort = A(I)

yields Asort = [0 - 1i 0 + 0i 0 + 1i 1 + 2i 3 + 1i]. sortrows sorts each row of an array first according to the first column, then breaks ties with the next column and so on until the whole array is sorted (Documentation here). The idea here is to make the first column the real component, and the second column the imaginary component.

For higher-dimensional arrays of complex numbers, you have a few options, but they'll all use the same principle as above. If you want to sort all elements of your matrix B, you can generate a sorted vector use the following code:

Bsep = [real(B(:)),imag(B(:))];
[~,I] = sortrows(Bsep);
SortedVec = B(I);

To reshape back into an 8x6 array (with sorted elements arranged down columns, from left to right), simply add this line:

Bsort = reshape(SortedVec,8,6);

Or, perhaps you want to sort each column of your array separately. Just use sortrows in a loop:

nCols = size(B,2);
Bsort = NaN(size(B));
for j = 1:nCols
   Bsep = [real(B(:,j)),imag(B(:,j))];
   [~,I] = sortrows(Bsep);
   Bsort(:,j) = B(I,j);
end

If you instead want to sort by the imaginary part first, then break ties with the real part, all you need to do is switch the position of the calls to real and imag in any of the code snippets above. For example,

clear
A = [1+2i 3+1i 1i 0 -1i];
AB = [imag(A); real(A)]; % note that real and imag are switched
[~,I] = sortrows(AB');
Asort = A(I)

yields Asort = [0 - 1i 0 + 0i 0 + 1i 3 + 1i 1 + 2i]



来源:https://stackoverflow.com/questions/43817030/sorting-the-complex-array-elements-with-imaginary-part-along-with-it-in-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!