Clear Filters
Clear Filters

Problem in sorting the elements of a complex column vetor in descending order.

1 view (last 30 days)
I am trying to sort the elements of a complex column vetor in descending order. The vector V is,
0.00192283028876372 + 2.81043109217511e-19i
0.00190176141109370 + 2.84699448762080e-19i
0.00147928037481547 - 2.74849838261212e-20i
0.00142769822781175 + 5.39004850343165e-20i
0.00117784972186107 + 2.08922253853130e-19i
0.00107796253204560 - 4.70645642035523e-20i
0.000800293022988111 + 1.22052572461947e-20i
0.000269899528923458 - 6.15654463989809e-20i
6.30592283269993e-05 - 7.50552502780072e-20i
2.23684164933882e-05 + 4.04939694038624e-21i
2.20073577954736e-05 - 5.80181618012595e-21i
1.95849840201760e-05 + 1.48445834874224e-20i
1.71925856406516e-05 - 1.15719050036467e-20i
1.28038961421133e-05 + 7.36148671834500e-20i
8.73059513582096e-06 + 1.23064435626381e-20i
6.28618594970652e-06 - 5.04267327744353e-20i
6.99991469893008e-08 - 1.02450891500650e-20i
2.80343931756963e-08 + 1.23048032017686e-20i
4.29713831923512e-09 + 3.73908633968205e-21i
2.28802604174547e-09 + 1.73500648980058e-20i
3.50507467296844e-10 - 1.10159115820306e-21i
7.32973689738923e-11 - 4.65099992985255e-21i
2.07808170074765e-11 - 2.84966374048803e-22i
4.68510672896002e-12 - 2.29503200085301e-21i
2.99051403313190e-12 + 3.62505072381834e-21i
1.32399364803625e-12 - 4.30261778360293e-21i
6.07212915777181e-13 + 2.79063028387683e-22i
1.23086553364893e-13 + 8.26560004261648e-21i
1.72392520759381e-14 + 1.86019192086409e-21i
1.01989680354965e-14 + 3.46136359467220e-21i
-8.05918955245067e-21 + 8.23317408693105e-22i
1.36822909718840e-15 - 1.33221528989010e-21i
I code as below,
% sort the variances in decreasing order
[junk, rindices] = sort(-1*V);
% V1=[2;6;4;3;1;8];
% [junk1, rindices1] = sort(-1*V1);
V = V(rindices);
However, after sorting, V became,
-8.05918955245067e-21 + 8.23317408693105e-22i
1.36822909718840e-15 - 1.33221528989010e-21i
1.01989680354965e-14 + 3.46136359467220e-21i
1.72392520759381e-14 + 1.86019192086409e-21i
1.23086553364893e-13 + 8.26560004261648e-21i
6.07212915777181e-13 + 2.79063028387683e-22i
1.32399364803625e-12 - 4.30261778360293e-21i
2.99051403313190e-12 + 3.62505072381834e-21i
4.68510672896002e-12 - 2.29503200085301e-21i
2.07808170074765e-11 - 2.84966374048803e-22i
7.32973689738923e-11 - 4.65099992985255e-21i
3.50507467296844e-10 - 1.10159115820306e-21i
2.28802604174547e-09 + 1.73500648980058e-20i
4.29713831923512e-09 + 3.73908633968205e-21i
2.80343931756963e-08 + 1.23048032017686e-20i
6.99991469893008e-08 - 1.02450891500650e-20i
6.28618594970652e-06 - 5.04267327744353e-20i
8.73059513582096e-06 + 1.23064435626381e-20i
1.28038961421133e-05 + 7.36148671834500e-20i
1.71925856406516e-05 - 1.15719050036467e-20i
1.95849840201760e-05 + 1.48445834874224e-20i
2.20073577954736e-05 - 5.80181618012595e-21i
2.23684164933882e-05 + 4.04939694038624e-21i
6.30592283269993e-05 - 7.50552502780072e-20i
0.000269899528923458 - 6.15654463989809e-20i
0.000800293022988111 + 1.22052572461947e-20i
0.00107796253204560 - 4.70645642035523e-20i
0.00117784972186107 + 2.08922253853130e-19i
0.00142769822781175 + 5.39004850343165e-20i
0.00147928037481547 - 2.74849838261212e-20i
0.00190176141109370 + 2.84699448762080e-19i
0.00192283028876372 + 2.81043109217511e-19i
which is defintely not in descending order.
I also tried on positive real numbers,
V1=[2;6;4;3;1;8];
[junk1, rindices1] = sort(-1*V1);
and the consequence is correct.
I don't know why the sorting of the complex numbers seems wrong.

Accepted Answer

Cris LaPierre
Cris LaPierre on 15 Nov 2021
Edited: Cris LaPierre on 15 Nov 2021
Just specify 'descend' for your sort order.
format longE
V=[0.00192283028876372 + 2.81043109217511e-19i
0.00190176141109370 + 2.84699448762080e-19i
0.00147928037481547 - 2.74849838261212e-20i
0.00142769822781175 + 5.39004850343165e-20i
0.00117784972186107 + 2.08922253853130e-19i
0.00107796253204560 - 4.70645642035523e-20i
0.000800293022988111 + 1.22052572461947e-20i
0.000269899528923458 - 6.15654463989809e-20i
6.30592283269993e-05 - 7.50552502780072e-20i
2.23684164933882e-05 + 4.04939694038624e-21i
2.20073577954736e-05 - 5.80181618012595e-21i
1.95849840201760e-05 + 1.48445834874224e-20i
1.71925856406516e-05 - 1.15719050036467e-20i
1.28038961421133e-05 + 7.36148671834500e-20i
8.73059513582096e-06 + 1.23064435626381e-20i
6.28618594970652e-06 - 5.04267327744353e-20i
6.99991469893008e-08 - 1.02450891500650e-20i
2.80343931756963e-08 + 1.23048032017686e-20i
4.29713831923512e-09 + 3.73908633968205e-21i
2.28802604174547e-09 + 1.73500648980058e-20i
3.50507467296844e-10 - 1.10159115820306e-21i
7.32973689738923e-11 - 4.65099992985255e-21i
2.07808170074765e-11 - 2.84966374048803e-22i
4.68510672896002e-12 - 2.29503200085301e-21i
2.99051403313190e-12 + 3.62505072381834e-21i
1.32399364803625e-12 - 4.30261778360293e-21i
6.07212915777181e-13 + 2.79063028387683e-22i
1.23086553364893e-13 + 8.26560004261648e-21i
1.72392520759381e-14 + 1.86019192086409e-21i
1.01989680354965e-14 + 3.46136359467220e-21i
-8.05918955245067e-21 + 8.23317408693105e-22i
1.36822909718840e-15 - 1.33221528989010e-21i];
newV = sort(V,'descend')
newV =
1.922830288763720e-03 + 2.810431092175110e-19i 1.901761411093700e-03 + 2.846994487620800e-19i 1.479280374815470e-03 - 2.748498382612120e-20i 1.427698227811750e-03 + 5.390048503431650e-20i 1.177849721861070e-03 + 2.089222538531300e-19i 1.077962532045600e-03 - 4.706456420355230e-20i 8.002930229881110e-04 + 1.220525724619470e-20i 2.698995289234580e-04 - 6.156544639898089e-20i 6.305922832699930e-05 - 7.505525027800721e-20i 2.236841649338820e-05 + 4.049396940386240e-21i 2.200735779547360e-05 - 5.801816180125950e-21i 1.958498402017600e-05 + 1.484458348742240e-20i 1.719258564065160e-05 - 1.157190500364670e-20i 1.280389614211330e-05 + 7.361486718345000e-20i 8.730595135820959e-06 + 1.230644356263810e-20i 6.286185949706520e-06 - 5.042673277443530e-20i 6.999914698930080e-08 - 1.024508915006500e-20i 2.803439317569630e-08 + 1.230480320176860e-20i 4.297138319235120e-09 + 3.739086339682050e-21i 2.288026041745470e-09 + 1.735006489800580e-20i 3.505074672968440e-10 - 1.101591158203060e-21i 7.329736897389230e-11 - 4.650999929852550e-21i 2.078081700747650e-11 - 2.849663740488030e-22i 4.685106728960020e-12 - 2.295032000853010e-21i 2.990514033131900e-12 + 3.625050723818340e-21i 1.323993648036250e-12 - 4.302617783602930e-21i 6.072129157771810e-13 + 2.790630283876830e-22i 1.230865533648930e-13 + 8.265600042616479e-21i 1.723925207593810e-14 + 1.860191920864090e-21i 1.019896803549650e-14 + 3.461363594672200e-21i 1.368229097188400e-15 - 1.332215289890100e-21i -8.059189552450671e-21 + 8.233174086931050e-22i
  3 Comments
Cris LaPierre
Cris LaPierre on 15 Nov 2021
Edited: Cris LaPierre on 15 Nov 2021
That is because your numbers are complex. You can find details here. By default, the sort function sorts complex values by their magnitude, and breaks ties using phase angles.
The magnitude is always positive. So while negating real numbers flips their order, that does not work for complex numbers because it doesn't change the sign of their magnitude.

Sign in to comment.

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Tags

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!