relabel the elements in each column according to the magnitude of these elements in an array
    5 Ansichten (letzte 30 Tage)
  
       Ältere Kommentare anzeigen
    
    Grace
 am 5 Jun. 2014
  
    
    
    
    
    Kommentiert: Star Strider
      
      
 am 7 Jun. 2014
            Hi, I have
 a=[4 5; 
    5 1;
    6 4; 
    7 7]
I want to relabel the elements in each column according to the magnitude of these elements in the column to become
   a=[1 3; 
      2 1;
      3 2; 
      4 4]
How can I do this? Thank you.
0 Kommentare
Akzeptierte Antwort
  Star Strider
      
      
 am 5 Jun. 2014
        
      Bearbeitet: Star Strider
      
      
 am 5 Jun. 2014
  
      This works:
a=[4 5; 5 1;6 4; 7 7]
[as,ai] = sort(a,1);
b = [ai(ai(:,1),1)  ai(ai(:,2),2)]
produces:
b =
       1     3
       2     1
       3     2
       4     4
It sorts the first and second columns independently. Then, to get the column indices as you want them, it maps each set of column indices to its own sorted indices. A bit complicated to explain, but taking the reference to the columns of b apart and looking at its components will reveal how it works.
12 Kommentare
  Star Strider
      
      
 am 7 Jun. 2014
				My pleasure!
It’s an unusual enough request that it took some time to come up with solution. When I was Answering another Question involving unique, it occurred to me that the unique function actually does exactly what you want.
Weitere Antworten (0)
Siehe auch
Kategorien
				Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!