MATLAB Answers

Matt J

Why does SORT outperform SORTROWS?

Asked by Matt J
on 30 Dec 2012

Any theories as to why SORT + row re-ordering outperforms SORTROWS when sorting with respect to a single column:

%Elapsed time is 3.589928 seconds.
   %Elapsed time is 14.869106 seconds.



No products are associated with this question.

1 Answer

Answer by Jan Simon
on 30 Dec 2012
Edited by Jan Simon
on 30 Dec 2012
 Accepted answer

The profile shows, that the additional time is spent in the C-Mex function sortrowsc.mexw64. At least in R2009a the source code has been shipped with Matlab. Inside the C-Mex, the not stable qsort is called, which is obviously slower than Matlab's sort algorithm.

Copy sortrows.m and change the line 74 of from:

ndx = sortrowsc(x_sub, col);


ndx = sort_back_to_front(x_sub, col);

to let the main work be done by sort. For a productive use, cells must be considered also, see some lines later for the general case in the else branch. It is ironic, that this is the "old Matlab 6.0" fallback, which has been obviously much faster.

Please send an enhancement request to TMW.


Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!