-6

Sorry for the confusion in my earlier post. I meant something else with my question:

I have an arry like this:

A = [2.4   1.2   4.1   3.1]

I am looking for a way to "rank" the elements in A. That is, when I sort this array (descending), I need the array of the ranks each element had in the original array A. So for example, 4.1 in A is the largest element and thus has rank 1. Element 3.1 is the second largest, so it has rank 2, and so on. So finally, I have this:

A =     [2.4   1.2   4.1   3.1]
Ranks = [ 3     4     1     2 ]

I am looking for a routine to find "Ranks" above. Any suggestions?

to_sam
  • 695
  • 8
  • 19
  • 2
    possible duplicate of [Sort a matrix with another matrix](http://stackoverflow.com/questions/2679022/sort-a-matrix-with-another-matrix) - this got asked a hundred times, also [here](http://stackoverflow.com/questions/13998098/matlab-sort-a-matrix-based-off-how-a-vector-is-sorted) – Robert Seifert Dec 02 '13 at 12:21
  • So if rank 1 is associated with the largest value, why not just sort the array? – darthbith Dec 03 '13 at 11:00
  • Because I must preserve the original order of A and at the same time know, which rank each element has in A. If I just sort A, the mapping between the original order of A and the rank gets lost. – to_sam Dec 03 '13 at 11:31

1 Answers1

0

Ok, I have the solution. I can find the rank through this command:

  [A_sorted, rank] = sort(A, 'descend');
  for k=1:10
      rank = find(A_sorted == A(k));
  end
to_sam
  • 695
  • 8
  • 19