在MATLAB中获取置换的映射 [英] Getting the mapping for a permutation in MATLAB
本文介绍了在MATLAB中获取置换的映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
说我有两个数组,其中一个是另一个的排列:
Say I have two arrays where one is a permutation of the other:
A = [2 1 5 3 7]
B = [7 2 1 3 5]
在两个数组中都没有重复.
with no repetitions in either array.
如何获取两者之间的置换映射?
How can I obtain the permutation mapping between both?
例如A->B
应为:
[2, 3, 5, 4, 1]
这意味着:
A(1) -> B(2)
A(2) -> B(3)
A(3) -> B(5)
A(4) -> B(4)
A(5) -> B(1)
更新:
是否有不使用ismember
的 fast 矢量化解决方案?以我的经验,对于很大的数组,ismember
往往很慢.
Update:
Is there a fast vectorized solution that does not use ismember
? In my experience, ismember
tends to be slow for very large arrays.
推荐答案
这是怎么回事:
[i a] = sort(A);
[i b] = sort(B);
mapping = b(a)
这篇关于在MATLAB中获取置换的映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文