Problem in sorting the elements of a complex column vetor in descending order.
    5 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
    奥 刘
 el 15 de Nov. de 2021
  
    
    
    
    
    Comentada: 奥 刘
 el 16 de Nov. de 2021
            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.
0 comentarios
Respuesta aceptada
  Cris LaPierre
    
      
 el 15 de Nov. de 2021
        
      Editada: Cris LaPierre
    
      
 el 15 de Nov. de 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')
3 comentarios
  Cris LaPierre
    
      
 el 15 de Nov. de 2021
				
      Editada: Cris LaPierre
    
      
 el 15 de Nov. de 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.
Más respuestas (0)
Ver también
Categorías
				Más información sobre Shifting and Sorting Matrices en Help Center y File Exchange.
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

