3 个答案:
答案 0 :(得分:2)
这是我的解决方案:
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包含big