Вот мое решение:
ordermat = [2; 1; 4; 3; 6];
bigmat = [
1 10
1 30
1 40
2 1
2 11
3 58
4 2
4 5
];
%#bigmat = sortrows(bigmat,1);
%# keep valid IDs,
ord = ordermat( ismember(ordermat,bigmat(:,1)) );
ord = grp2idx(ord);
%# starting/ending locations of the different IDs in bigmat
startInd = find( diff([0;bigmat(:,1)]) );
endInd = [startInd(2:end)-1; size(bigmat,1)];
%# generate startInd(i):endInd(i) intervals
ind = arrayfun(@colon, startInd, endInd, 'UniformOutput',false);
%# order then combine the intervals of indices
ind = [ind{ord}];
%# get final sorted result
finalans = bigmat(ind,:);
Я убедился, что он обрабатывает различные случаи, такие как:
ordermat
содержит идентификаторы, не найденные в bigmat
: ordermat = [2;1;4;3;6]
- не все идентификаторы
bigmat
представлены в ordermat
: ordermat = [2;1]
- Идентификаторы не последовательные и / или не начинаются с 1:
ordermat=ordermat+10; bigmat=bigmat+10;