生成某些排列(Generating certain permutations)

编程入门 行业动态 更新时间:2024-10-09 14:21:48
生成某些排列(Generating certain permutations)

我有2个变量......输入数量N和历史记录M的长度。这两个变量决定了矩阵V的大小,即n×m,即n行,m列。

我很难想出一个算法,它使我能够产生一定量的排列(或序列,你如何看待适合)。

如果有人能够用算法帮助我,我会非常高兴,如果可能的话,在Matlab中,但是伪算法也是非常好的。

我给你3个例子:

如果输入的数量是N = 1并且历史的长度是M = 2,那么我有(M + 1)^ N个不同的组合,在这种情况下是3.排列是:

(如果您对matlab矩阵符号不熟悉,单独列;分隔行)。

V(1) = [1,0,0] V(2) = [0,1,0] V(3) = [0,0,1] 如果输入的数量是N = 2并且历史的长度是M = 2,则我有(M + 1)^ N个不同的组合,在这种情况下为9。

排列如下:

V(1) = [1,0,0; 1,0,0] V(2) = [1,0,0; 0,1,0] V(3) = [1,0,0; 0,0,1] V(4) = [0,1,0; 1,0,0] V(5) = [0,1,0; 0,1,0] V(6) = [0,1,0; 0,0,1] V(7) = [0,0,1; 1,0,0] V(8) = [0,0,1; 0,1,0] V(9) = [0,0,1; 0,0,1] 如果输入的数量是N = 3并且历史的长度是M = 3,则我有(M + 1)^ N个不同的组合,在这种情况下是64。

排列如下:

V(1) = [1,0,0,0; 1,0,0,0; 1,0,0,0] V(2) = [1,0,0,0; 1,0,0,0; 0,1,0,0] V(3) = [1,0,0,0; 1,0,0,0; 0,0,1,0] V(4) = [1,0,0,0; 1,0,0,0; 0,0,0,1] V(5) = [1,0,0,0; 0,1,0,0; 1,0,0,0] ... V(8) = [1,0,0,0; 0,1,0,0; 0,0,0,1] V(9) = [1,0,0,0; 0,0,1,0; 1,0,0,0] ... V(16) = [1,0,0,0; 0,0,0,1; 0,0,0,1] V(17) = [0,1,0,0; 1,0,0,0; 1,0,0,0] ... V(64) = [0,0,0,1; 0,0,0,1; 0,0,0,1]

编辑:我只是找到一种方法来产生真正的大矩阵W,其中每一行代表V(i)

对于第一种情况:

W = eye(3)

这里eye(k)创建一个大小为kxk的单位矩阵

对于第二种情况:

W = [kron(eye(3), ones(3,1)), ... kron(ones(3,1), eye(3))]

这里kron是kronecker产品 ,并且ones(k,l) 创建了一个大小为kxl 的矩阵

对于第三种情况:

W = [kron(kron(eye(4), ones(4,1)), ones(4,1)), ... kron(kron(ones(4,1), eye(4)), ones(4,1)), ... kron(kron(ones(4,1), ones(4,1)), eye(4))]

现在我们创建了矩阵W,其中每行代表矢量形式的V(i),V(i)还不是矩阵。

观察两件事情:

当输入N增加时,额外的列被添加额外的克罗内克积,单位矩阵沿着向量移动。 当历史M的长度增加时,身份矩阵向量增加,例如,眼睛(4) - >眼睛(5),(4,1) - >个(5,1)。

I have 2 variables... the number of inputs N and the length of the history M. These two variables determine the size of the matrix V which is n x m, i.e., n rows, m columns.

I have difficulties to come up with a algorithm which enables me to generate a certain amount of permutations (or sequences, how you see fit).

I would be really glad if someone could help me with a algorithm, if possible in Matlab, but a pseudo-algorithm would also be very nice.

I give you 3 examples:

If the number of inputs is N = 1 and the length of the history is M = 2, I have (M+1)^N different combinations, in this case 3. The permutations are:

(In case you are not familiar with matlab matrix notation, , seperates columns, ; seperates rows.)

V(1) = [1,0,0] V(2) = [0,1,0] V(3) = [0,0,1] If the number of inputs is N = 2 and the length of the history is M = 2, I have (M+1)^N different combinations, in this case 9.

The permutations are:

V(1) = [1,0,0; 1,0,0] V(2) = [1,0,0; 0,1,0] V(3) = [1,0,0; 0,0,1] V(4) = [0,1,0; 1,0,0] V(5) = [0,1,0; 0,1,0] V(6) = [0,1,0; 0,0,1] V(7) = [0,0,1; 1,0,0] V(8) = [0,0,1; 0,1,0] V(9) = [0,0,1; 0,0,1] If the number of inputs is N = 3 and the length of the history is M = 3, I have (M+1)^N different combinations, in this case 64.

The permutations are:

V(1) = [1,0,0,0; 1,0,0,0; 1,0,0,0] V(2) = [1,0,0,0; 1,0,0,0; 0,1,0,0] V(3) = [1,0,0,0; 1,0,0,0; 0,0,1,0] V(4) = [1,0,0,0; 1,0,0,0; 0,0,0,1] V(5) = [1,0,0,0; 0,1,0,0; 1,0,0,0] ... V(8) = [1,0,0,0; 0,1,0,0; 0,0,0,1] V(9) = [1,0,0,0; 0,0,1,0; 1,0,0,0] ... V(16) = [1,0,0,0; 0,0,0,1; 0,0,0,1] V(17) = [0,1,0,0; 1,0,0,0; 1,0,0,0] ... V(64) = [0,0,0,1; 0,0,0,1; 0,0,0,1]

Edit: I just found a way to generate really large matrices W in which each row represents V(i)

For the first case:

W = eye(3)

Herein eye(k) creates an identity matrix of size k x k

For the second case:

W = [kron(eye(3), ones(3,1)), ... kron(ones(3,1), eye(3))]

Herein kron is the kronecker product, and ones(k,l) creates a matrix with ones of size k x l

For the third case:

W = [kron(kron(eye(4), ones(4,1)), ones(4,1)), ... kron(kron(ones(4,1), eye(4)), ones(4,1)), ... kron(kron(ones(4,1), ones(4,1)), eye(4))]

Now we have created the matrices W in which each row represents V(i) in vector form, V(i) is not yet a matrix.

Observe two things:

When the input N is increased an extra column is added with an extra kronecker product and the identity matrix moves along the vector. When the length of the history M is increased the identity matrices vectors are increased, e.g., eye(4) -> eye(5), ones(4,1) -> ones(5,1).

最满意答案

我想这可以满足你的所有要求。 即使顺序似乎对我来说是正确的:

M=3;N=3; mat1=eye(M+1); vectors=mat2cell(repmat(1:M+1,N,1),ones(N,1),[M+1]);

超高效的笛卡尔产品,从这里采取:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% n = numel(vectors); %// number of vectors combs = cell(1,n); %// pre-define to generate comma-separated list [combs{end:-1:1}] = ndgrid(vectors{end:-1:1}); %// the reverse order in these two %// comma-separated lists is needed to produce the rows of the result matrix in %// lexicographical order combs = cat(n+1, combs{:}); %// concat the n n-dim arrays along dimension n+1 combs = reshape(combs,[],n); %// reshape to obtain desired matrix %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% V=cell(size(combs,1),1); for i=1:size(combs,1) for j=1:size(combs,2) V{i,1}=[V{i,1};mat1(combs(i,j),:)]; end end

输出:

M=2,N=2; V= [1,0,0;1,0,0] [1,0,0;0,1,0] [1,0,0;0,0,1] [0,1,0;1,0,0] [0,1,0;0,1,0] [0,1,0;0,0,1] [0,0,1;1,0,0] [0,0,1;0,1,0] [0,0,1;0,0,1] M=3;N=3; %order verified for the indices given in the question V(1) = [1,0,0,0; 1,0,0,0; 1,0,0,0] V(2) = [1,0,0,0; 1,0,0,0; 0,1,0,0] V(3) = [1,0,0,0; 1,0,0,0; 0,0,1,0] V(4) = [1,0,0,0; 1,0,0,0; 0,0,0,1] V(5) = [1,0,0,0; 0,1,0,0; 1,0,0,0] ... V(8) = [1,0,0,0; 0,1,0,0; 0,0,0,1] V(9) = [1,0,0,0; 0,0,1,0; 1,0,0,0] ... V(16) = [1,0,0,0; 0,0,0,1; 0,0,0,1] V(17) = [0,1,0,0; 1,0,0,0; 1,0,0,0] ... V(64) = [0,0,0,1; 0,0,0,1; 0,0,0,1]

I guess this satisfies all your requirements. Even the order seems correct to me:

M=3;N=3; mat1=eye(M+1); vectors=mat2cell(repmat(1:M+1,N,1),ones(N,1),[M+1]);

Super-efficient cartesian product, taken from here:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% n = numel(vectors); %// number of vectors combs = cell(1,n); %// pre-define to generate comma-separated list [combs{end:-1:1}] = ndgrid(vectors{end:-1:1}); %// the reverse order in these two %// comma-separated lists is needed to produce the rows of the result matrix in %// lexicographical order combs = cat(n+1, combs{:}); %// concat the n n-dim arrays along dimension n+1 combs = reshape(combs,[],n); %// reshape to obtain desired matrix %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% V=cell(size(combs,1),1); for i=1:size(combs,1) for j=1:size(combs,2) V{i,1}=[V{i,1};mat1(combs(i,j),:)]; end end

Outputs:

M=2,N=2; V= [1,0,0;1,0,0] [1,0,0;0,1,0] [1,0,0;0,0,1] [0,1,0;1,0,0] [0,1,0;0,1,0] [0,1,0;0,0,1] [0,0,1;1,0,0] [0,0,1;0,1,0] [0,0,1;0,0,1] M=3;N=3; %order verified for the indices given in the question V(1) = [1,0,0,0; 1,0,0,0; 1,0,0,0] V(2) = [1,0,0,0; 1,0,0,0; 0,1,0,0] V(3) = [1,0,0,0; 1,0,0,0; 0,0,1,0] V(4) = [1,0,0,0; 1,0,0,0; 0,0,0,1] V(5) = [1,0,0,0; 0,1,0,0; 1,0,0,0] ... V(8) = [1,0,0,0; 0,1,0,0; 0,0,0,1] V(9) = [1,0,0,0; 0,0,1,0; 1,0,0,0] ... V(16) = [1,0,0,0; 0,0,0,1; 0,0,0,1] V(17) = [0,1,0,0; 1,0,0,0; 1,0,0,0] ... V(64) = [0,0,0,1; 0,0,0,1; 0,0,0,1]

更多推荐

本文发布于:2023-08-07 12:36:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464312.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:排列   Generating   permutations

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!