MATLAB Answers

0

ranking of vector with non-unique elements

I need to perform fast ranking of the vector with non-unique elements, like in the following example:
F = [10.1 31.0 20.5 20.5 10.1] -> rankF = [3 1 2 2 3]
So far I am using the following code:
[~,~,iFu] = unique(F,'stable');
[~,iFs] = sort(F,'descend');
[~,iFs] = sort(iFs);
[~, ~, rankF] = unique(iFs(iFu));
But, I am not sure if this code is really effective. Is there any better (faster) way how to solve this problem?

  0 Comments

Sign in to comment.

Products


Release

R2019a

1 Answer

回答者: Johannes Fischer 2019 年 9 月 18 日
 採用された回答

So you want to order starting with the highest value?
F = [10.1 31.0 20.5 20.5 10.1]
[~, ~, rankF] = unique(max(F)-F)

  4 Comments

Show 1 older comment
+1 I like it.
But the MAX is not needed.
[~, ~, rankF] = unique(-F)
Even better ... thanks Bruno!

Sign in to comment.