eal*_*iaj 2 algorithm matlab permutation representation
我最近读到了关于Lehmer代码以及它们如何用于将索引映射到与该索引相对应的排列,并且意识到它们对于快速生成排列非常有用.有谁知道如何使用算法完成这个,以及这种方法的限制是什么,我想我们不能超过index = 1.7977e + 308,但似乎仍然是一个非常有趣的方法.
所以基本上可以说我们有
perm
1 0 0 0
2 0 0 1
3 0 0 2
4 0 1 0
5 0 1 1
6 0 1 2
...
Run Code Online (Sandbox Code Playgroud)
我们应该能够推断出[0 1 0]的索引是4,或者索引6对应于[0 1 2]
谢谢你的帮助.
每个索引的向量是索引的基数3表示(减1)函数dec2base
,base2dec
可用于此,只需稍微调整即可将sting输出转换为所需格式
index=4; % input index
n=3; % length of vector
vec=str2num([dec2base(index-1,3,n)].').'
vec=
0 1 0
Run Code Online (Sandbox Code Playgroud)
vec=[0,1,2]; % input vector
vecstr=strcat(['' vec(:)+'0'].');
index=base2dec(vecstr,3)+1
index =
6
Run Code Online (Sandbox Code Playgroud)