Sorting a complex array and its derivatives

2 views (last 30 days)
Dear all,
There is a complex 2D-array X, where each row should be sorted according to the absolute values within the row. Moreover, another array ( angle1=angle(X)) should be sorted in the same way (ranged according to the absolute values within the row).
Here is an example:
X=[14.3353097177036 + 0.368132698534547i,0.0778904302845413 - 20.6057085094919i,-14.3353097177036 - 0.368132698534547i,-0.0778904302845413 + 20.6057085094919i;8.35480739256033 + 11.6726455859351i,16.4810429757788 - 12.3489579854284i,-8.35480739256033 - 11.6726455859351i,-16.4810429757788 + 12.3489579854284i;19.7759267690159 + 5.70662750381570i,4.28306576088118 - 13.7160457900973i,-19.7759267690159 - 5.70662750381570i,-4.28306576088118 + 13.7160457900973i;13.5402031155189 - 4.85415907641717i,7.36014471986172 + 19.2099850443929i,-13.5402031155189 + 4.85415907641717i,-7.36014471986172 - 19.2099850443929i;12.0467821469972 + 7.88699653993500i,10.8882591549695 - 17.4411711698051i,-12.0467821469972 - 7.88699653993500i,-10.8882591549695 + 17.4411711698051i];
angle1=angle(X);
[~,idx]=sort(abs(X),2);
angle2=NaN.*X;
for zz=1:5
angle2(zz,:)=angle(X(zz,idx(zz,:)));
end
angle3=angle1(idx);
So, the correct answer is delieved in angle2 based on for loop.
I was expecting that array of indices in every row (variable idx) would do the same job, but without for loop, however, angle3 just contains 4 values (1st,2nd, 3rd,4th of the original array), which is though logical because idx has only integer values 1..4.
Is there an elegant way to solve this problem?
Thank you in advance!

Answers (1)

Ankita Bansal
Ankita Bansal on 18 Jun 2018
Edited: Ankita Bansal on 18 Jun 2018
You can write
B = sort(X,2,'ComparisonMethod','abs')
angle1=angle(B);

Categories

Find more on Shifting and Sorting Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!