How do we sort scalar value in ranks?
    8 Ansichten (letzte 30 Tage)
  
       Ältere Kommentare anzeigen
    
Hello everyone, I have struct R bellow, I sort these sequence in ascending order according to Weights values, Now I want to rank them, if weights values are the same so I put them in the same rank and have the same value:
R:
sequence          Cost              Weights   Rank
1x197 double	[4;0;0;184]	188	         1
1x197 double	[4;0;0;189]	193	         1
1x197 double	[4;0;0;189]	193	         1
1x197 double	[4;0;0;190]	194	         1
1x197 double	[4;0;0;191]	195         	1
1x197 double	[4;0;0;192]	196	        1
1x197 double	[4;0;0;192]	196	        1
The result that I want to obtain is this
sequence          Cost              Weights   Rank
1x197 double	[4;0;0;184]	188	         1
1x197 double	[4;0;0;189]	193	         2
1x197 double	[4;0;0;189]	193	         2
1x197 double	[4;0;0;190]	194	         3
1x197 double	[4;0;0;191]	195         	4
1x197 double	[4;0;0;192]	196	        5
1x197 double	[4;0;0;192]	196	        5
Thank you
0 Kommentare
Akzeptierte Antwort
  Sanman
    
 am 25 Mai 2021
        Hi Mira,
Since you already have your structure field “weights” sorted, you can make use of “unique” function here. MATLAB function “unique” returns unique values in array, along with the rank as well. 
The following command should return the rank of the weights as you expect.
>> [~,~,rank] = unique(R.Weights)
You can also refer to this documentation page for further reference.  
 https://www.mathworks.com/help/matlab/ref/double.unique.html
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
				Mehr zu Shifting and Sorting 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!

