sort and assign ranking

41 ビュー (過去 30 日間)
Gokturk
Gokturk 2013 年 1 月 21 日
回答済み: TjeerdB 2017 年 10 月 27 日
Hi I need to sort a vector and assign a ranking for the corresponding sorting order. I'm using sort function [sortedValue_X , X_Ranked] = sort(X,'descend'); but the problem is it assignes different ranks for the same values (zeros). ie x = [ 13 15 5 5 0 0 0 1 0 3] and i want zeros to take the same last rank which is 6 and fives needs to share the 3rd rank etc.. any suggestions?

採用された回答

Image Analyst
Image Analyst 2013 年 1 月 21 日
You can use the third return argument of unique(): Try this:
X = [ 13 15 5 5 0 0 0 1 0 3]
[sortedValue_X , X_Ranked] = sort(X,'descend')
[uniqueValues, ia, ic]=unique(sortedValue_X)
X_Ranked = max(ic)-ic+1 % <- there's your answer!
In command window:
X_Ranked =
1 2 3 3 4 5 6 6 6 6
  1 件のコメント
Gokturk
Gokturk 2013 年 1 月 25 日
Hi, I do have a small but an important issue, X_ranked vector gives sorted values, points should be in their places, X_Ranked should be [1 2 3 3 6 6 6 5 6 4] can you pls help on this? many thanks

サインインしてコメントする。

その他の回答 (1 件)

TjeerdB
TjeerdB 2017 年 10 月 27 日
X = [13 15 5 5 0 0 0 1 0 3];
[temp,X_ranked] = ismember(X,unique(X))
In command window:
X_ranked =
5 6 4 4 1 1 1 2 1 3

カテゴリ

Find more on Shifting and Sorting Matrices in Help Center and File Exchange

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by