sorting - MATLAB assign rank to values in a matrix in ascending/descending order -


i have 60x6 matrix x column 1 index , columns 2 6 data particular index. i'd assign ranks each value in data columns, such columns 2 though 5 ranked in descending order (highest value ranked 1, lowest value ranked 60) , column 6 ranked in descending order (lowest value ranked 1, highest ranked 60), row order maintained according the index (column 1). rank ties assigned rank equal average of positions in ascending order of values.

i've looked through number of suggestions (here, here , here) , tried own version:

[~,z1] = sort(x(:,2),'descend'); [~,z2] = sort(x(:,3),'descend'); [~,z3] = sort(x(:,4),'descend'); [~,z4] = sort(x(:,5),'descend'); [~,z5] = sort(x(:,6)); 

but none seem work way want them to. for

    x =          1    0.9503    0.5646    0.3785    0.5468   -0.0161              2    0.9430    0.5728    0.3320    0.6693   -0.0161         3    0.5305    0.2719    0.1545    0.3480    0.0042         4    0.8588    0.5816    0.3429    0.6477   -0.0158         5    0.9391    0.5984    0.3362    0.6054   -0.0161         6    0.3780    0.2307    0.0906    0.3387    0.0054 

i need

    ans =            1   1   4   1   4   2          2   2   3   4   1   2          3   5   5   5   5   5          4   4   2   2   2   4          5   3   1   3   3   2          6   6   6   6   6   6 

i've found clumsy, workable solution problem.

it involves extracting, sorting , assigning ranks each column of data separately, re-combining ranks in table.

arank = sortrows(x(:,1:2),-2);       % sort data in descending (or ascending) order arank(:,3)=1:60;                     % assign rank arank = sortrows(arank,1);           % re-sort index % % same process applied individually each column allranks = horzcat(arank(:,[1,3]), brank(:,3),...  crank(:,3), drank(:,3), erank(:,3)); % combine ranks sorted index 

however, altogether adds 20 lines of code, more elegant alternatives received!


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -