开发者

sort vs sortrows in Matlab

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        开发者_Go百科   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!


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜