开发者

sort vs sortrows in Matlab

开发者 https://www.devze.com 2023-03-13 04:24 出处:网络
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 vectori

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

精彩评论

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

关注公众号