Return rank value of a row

10 ビュー (過去 30 日間)
Emma Kuttler
Emma Kuttler 2019 年 11 月 18 日
コメント済み: Emma Kuttler 2019 年 11 月 18 日
I have a 296x1 column vector called Sscore. Each row represents the score for a node, ie row 1 represents the score for node 1.
I want to return another column vector with the rank of each row, for example, if node 5 has the 10th highest score, i want the 5th row of that new vector to have a value of 10.
My code leading up to Sscore is below. Thanks!
% weight the matrix, giving a weight to each column
weights = [0.1666667, 0.1666667, 0.1666667, 0.1666667, 0.1666667, 0.1666667];
normalimpactw = bsxfun(@times, normalimpact, weights);
max_vals = max(normalimpactw); % maximum values of each colum(1 to 6 )
Dplus = zeros(size(normalimpactw,1), 1); % initialize
for ii =1: size(normalimpactw, 1 )
Dplus(ii, :) = sqrt(sum([normalimpactw(ii,:)-max_vals].^2 ));
end
Dplus; % column matrix after calculation
min_vals = min(normalimpactw); % minimim values of each colum(1 to 6 )
Dminus = zeros(size(normalimpactw,1), 1); % initialize
for ii =1: size(normalimpactw, 1 )
Dminus(ii, :) = sqrt(sum([normalimpactw(ii,:)-min_vals].^2 ));
end
Dminus; % column matrix after calculation
Sscore = zeros(size(Dminus,1),1);
for n=1:296
Sscore(n,:) = Dminus(n,:)/(Dminus(n,:)+Dplus(n,:));
end
Sscore;
  1 件のコメント
Adam Danz
Adam Danz 2019 年 11 月 18 日
The 2nd output to sort() is what you're describing. I didn't dig into your code so I'm not sure how your goal differs from sort().
Sscore = randi(100,296,1);
[~, rank] = sort(Sscore);

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

採用された回答

the cyclist
the cyclist 2019 年 11 月 18 日
編集済み: the cyclist 2019 年 11 月 18 日
[~,idx] = sort(Sscore,'desc');
[~,rank]= sort(idx)

その他の回答 (1 件)

the cyclist
the cyclist 2019 年 11 月 18 日
Here is another solution:
[~,rank] = ismember(Sscore,sort(Sscore,'desc'));
  2 件のコメント
the cyclist
the cyclist 2019 年 11 月 18 日
This answer is slightly preferable to me, because it handles ties in the conventional fashion of assigning equal rank. For example
Sscore = [6 4 1 2 6 7];
% sort-sort
[~,idx] = sort(Sscore,'desc');
[~,rank1]= sort(idx)
% ismember-sort
[~,rank2] = ismember(Sscore,sort(Sscore,'desc'))
gives
rank1 =
2 4 6 5 3 1
rank2 =
2 4 6 5 2 1
Notice that the two entries with Sscore=6 are both assigned second place, and there is no 3rd place.
Emma Kuttler
Emma Kuttler 2019 年 11 月 18 日
This works well, thanks!

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

カテゴリ

Help Center および File ExchangeParticle & Nuclear Physics についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by