フィルターのクリア

Ranking row values of matrix without adjustment

4 ビュー (過去 30 日間)
Tommaso Fornaciari
Tommaso Fornaciari 2016 年 12 月 4 日
コメント済み: Jos (10584) 2016 年 12 月 5 日
Hello, I have a matrix that is 8000x600 and I need an output matrix of the same dimensions, containing the ranks of each element per row. I have used tiedrank but this averages the two ranks of equal observations and assigns to both that rank, which is not useful for me.
To make it simple, if I have [ 7 6 2 9 2] I would like the output to have [4 3 1 5 2] so that the value "2" gets 2 different ranks and not the average as [4 3 1.5 5 1.5].
Thank you in advance for your help

回答 (1 件)

Jos (10584)
Jos (10584) 2016 年 12 月 4 日
for a vector:
V = [ 7 6 2 9 2]
[~,r] = sort(V)
rankV(r) = 1:numel(V)
for a matrix, you can loop over the columns
M = magic(5)
[~,r] = sort(M)
szM = size(M)
rankM = zeros(szM) % pre-allocation
ridx = 1:szM(1)
for k=1:szM(2)
rankM(r(:,k),k) = ridx
end
Other approaches using, for instance, ndgrid and sub2ind are also fine but less readable, I think.
  2 件のコメント
David Goodmanson
David Goodmanson 2016 年 12 月 4 日
For a matrix you can also do
[~,a] = sort(M);
[~,rankM] = sort(a);
Jos (10584)
Jos (10584) 2016 年 12 月 5 日
Yes, sure, I just overlooked that. Thanks, David!

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

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by