2
votes

Is it possible to achieve the same functionality with sort function than using sortrows. My matrix has over 4million+ rows and sortrows is bringing in a lot of latency because of iterations. (a vectorizated approach would be appreciated)

%Col1 -> date, Col2 -> id, Col3 -> ranking within each date-group (to help you debug)
data = [ ...
       734614 5 3; 734615 6 5; 734622 1 1; 734615 1 1; 734615 4 3; 
       734622 2 2; 734622 4 3;   734615 3 2; 734615 5 4; 734614 3 2; 
       734614 1 1; 734622 8 4; 734622 9 5;] ;

sortedanswer = 
  734614           1           1
  734614           3           2
  734614           5           3
  734615           1           1
  734615           3           2
  734615           4           3
  734615           5           4
  734615           6           5
  734622           1           1
  734622           2           2
  734622           4           3
  734622           8           4
  734622           9           5

Thanks!

1
Just to clarify, you are sorting by first column and then by the second column for identical first-column values? - Phonon

1 Answers

1
votes

You could do it as

[~,indx]=sort(data(:,1));
sortedanswer=data(indx,:)

sortedanswer =

      734614           5           3
      734614           3           2
      734614           1           1
      734615           6           5
      734615           1           1
      734615           4           3
      734615           3           2
      734615           5           4
      734622           1           1
      734622           2           2
      734622           4           3
      734622           8           4
      734622           9           5

Note that it is sorted by the rows in the first column. The order of the rows is the same as that in the original data, which is why you see 5 3 in the second and third columns in the first row in mine.