Я продолжал стучать в это, но не мог получить это быстрее, чем метод sortrows. Это использует тот факт, что каждая пара ключей уникальна, о чем я не упомянул выше.
% This gives us unique rows of integers between one and 10000, sorted first
% by column 1 then 2.
x = unique(uint32(ceil(10000*rand(1e6,2))),'rows');
tic;
idx = zeros(size(x,1),1);
% Work out where each group of the second keys will start in the sorted output.
StartingPoints = cumsum([1;accumarray(x(:,2),1)]);
% Work out where each group of the first keys is in the input.
Ends = find([~all(diff(x(:,1),1,1)==0,2);true(1,1)]);
Starts = [1;Ends(1:(end-1))+1];
% Build the index.
for i = 1:size(Starts)
temp = x(Starts(i):Ends(i),2);
idx(StartingPoints(temp)) = Starts(i):Ends(i);
StartingPoints(temp) = StartingPoints(temp) + 1;
end
% Apply the index.
y = x(idx,:);
toc
tic;
z = sortrows(x,2);
toc
isequal(y,z)
Дает 0,21 секунды для моего алгоритма и 0,18 для секунды (стабильно при разных случайных затравках).
Если кто-нибудь увидит дальнейшее ускорение (кроме mex), пожалуйста, не стесняйтесь добавлять.