How to give ranking from highest to lowest

Hello, I have number like
Data=[5 6 9 1 5 2]
I want to rank them as: [3 2 1 6 4 5] Can any please help me How can I do this. Thanks in advance.

1 Comment

i got a average accuracy 79% at rank 1
from rank 2 to rank 10 , i need to find different average accuracy that should be greater than 79%
please give me the code regarding this problem

Sign in to comment.

 Accepted Answer

This should give you the rank you are asking for, Mekala:
[~,p] = sort(Data,'descend');
r = 1:length(Data);
r(p) = r;
'r' will be the ranking.

1 Comment

Data=[5 6 9 1 5 2]
Data = 1×6
5 6 9 1 5 2
% I want to rank them as: [3 2 1 6 4 5]
[~,p] = sort(Data,'descend');
r = 1:length(Data);
r(p) = r
r = 1×6
3 2 1 6 4 5

Sign in to comment.

More Answers (4)

Data=[5 6 9 1 5 2]
[sd,r]=sort(Data,'descend')
sd % sorted data
r % the corresponding indices

5 Comments

No, sir, by this code I am getting rank as 3 2 1 5 6 4 but I want as 3 2 1 6 4 5
Thanks and please help
You said from the highest to the lowest:
sd = 9 6 5 5 2 1
r = 3 2 1 5 6 4
your result is false, because the fifth element is not the lowest
@Azzi: The 'rank' is the inverse of the index vector that 'sort' returns. That is, in the requested 3 2 1 6 4 5, the 6 in position 4 indicates that the element in the 4th position of Data is of the 6th rank, the 5 in position 6 indicates that the 6th position in Data is of the 5th rank, etc. Thus the positions and ranks are reversed from sort's returned 2nd argument.
Ok Roger, I understand now.
We can get the result by sorting the indices resulting from the first sort
Data=[5 6 9 1 5 2]
[~,ii]=sort(Data,'Descend')
[~,r]=sort(ii)

Sign in to comment.

Data =[4 8 9 4 7 4]
Data = 1×6
4 8 9 4 7 4
Kal = sort(Data , 'Descend')
Kal = 1×6
9 8 7 4 4 4
Kapil =sort(Kal)
Kapil = 1×6
4 4 4 7 8 9
Here's one that assigns the same rank to same values (ties):
Data=[5 6 9 1 5 2];
[C,~,ic] = unique(Data,'sorted'); % ic are ranks from lowest to highest ; C are unique values
r=(1+max(ic)-ic); % r: rank (highest receives 1; lowest receives length(C); tied values receive same rank)
[Data;r']
ans = 2×6
5 6 9 1 5 2 3 2 1 5 3 4
A=ones(4);
m=length(A);
r=rank(A)
r = 1

2 Comments

That rank() is different from what OP is asking about.
By "ranking" he really meant sorting. Your solution does not give the answer of [3 2 1 6 4 5] that he asked for.

Sign in to comment.

Categories

Community Treasure Hunt

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

Start Hunting!