matlab矩阵按指定顺序排序,根据第二个矩阵(MATLAB)中给出的顺序对矩阵进行排序...

这篇博客讨论了如何在MATLAB中根据第二个矩阵的顺序对一个矩阵进行排序。给出了三种不同的解决方案,涉及使用ismember、sortrows、look-up表、permutation等函数来实现按指定顺序排序。
摘要由CSDN通过智能技术生成

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值