matlab全排列 急!!

我现在又11列,每列有2个数,现在我想把这11列的数进行全排列,就是每列每次都拿出一个数进行组合。求高手啊!!!急!

你的这个是2行11列,要全排列方式为;首先要定义这个矩阵才行。
perms(a(1,:))和perms(a(2,:)),举个例子吧:
a=[1 2 3;4 5 6]

a =

1 2 3
4 5 6

>> perms(a(1,:))

ans =

3 2 1
3 1 2
2 3 1
2 1 3
1 2 3
1 3 2

>> perms(a(2,:))

ans =

6 5 4
6 4 5
5 6 4
5 4 6
4 5 6
4 6 5
我相信你明白了,呵呵。追问

你好,谢谢你的回答,可能是我说的不清楚。我是想要这样的,如下:
a=[1 2 3; 4 5 6]

怎么让他变成
1 2 3
1 2 6
1 5 3
1 5 6
4 2 3
4 2 6
4 5 3
4 5 6

追答

你这个是什么排列啊

追问

算是组合吧

追答

就是第一列不变 第二三列组合吗

追问

就是第几列的数据必须在第几列,不能换列,在这个前提下,进行组合,比如有11列,每列有2个数,那么应该有2^11种组合。

追答

给你解决了,很费劲的呀,呵呵。代码及结果为:(有什么不明白的欢迎继续追问)
a=[1 2 3; 4 5 6];
[m,n]=size(a);
aaa=[];
for i=1:n
b=perms(a(:,i));
c=b';
for j=1:factorial(m)
aa=[];
for k=1:(i-1)
aa=[aa a(:,k)];
end
aa=[aa c(:,j)];
for k=i+1:n
aa=[aa a(:,k)];
end
aaa=[aaa;aa];
aaa=unique(aaa,'rows');
end
end
aaa

aaa =

1 2 3
1 2 6
1 5 3
1 5 6
4 2 3
4 2 6
4 5 3
4 5 6

温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-05-14
function strc=funstr2(str1,str2)
clc,
%检验参数是否是字符串
ls1=length(str1);
for i=1:ls1
if ~ischar(str1(i))
error('Input must be string');
end
end
ls2=length(str2);
for i=1:ls2
if ~ischar(str2(i))
error('Input must be string');
end
end
slen=ls1+ls2;
A=NCHOOSEK(1:slen,ls1);
[m,n]=size(A);
A1=zeros(m,slen);
for i=1:m
for j=1:n
A1(i,A(i,j))=1;
end
end
strc=cell(m,1);
for ir=1:m
ic=1;
i1=0;
i2=0;
output=[];
while ic<=slen;
if(i1<=ls1)&(A1(ir,ic)==1);
i1=i1+1;
output(ic)=str1(i1);
else if (i2<=ls2)&(A1(ir,ic)==0)
i2=i2+1;
output(ic)=str2(i2);
end
end
ic=ic+1;
end
strc=char(output);
end
第2个回答  2011-05-14
function strc=funstr2(str1,str2)
clc,
%检验参数是否是字符串
ls1=length(str1);
for i=1:ls1
if ~ischar(str1(i))
error('Input must be string');
end
end
ls2=length(str2);
for i=1:ls2
if ~ischar(str2(i))
error('Input must be string');
end
end
slen=ls1+ls2;
A=NCHOOSEK(1:slen,ls1);
[m,n]=size(A);
A1=zeros(m,slen);
for i=1:m
for j=1:n
A1(i,A(i,j))=1;
end
end
strc=cell(m,1);
for ir=1:m
ic=1;
i1=0;
i2=0;
output=[];
while ic<=slen;
if(i1<=ls1)&(A1(ir,ic)==1);
i1=i1+1;
output(ic)=str1(i1);
else if (i2<=ls2)&(A1(ir,ic)==0)
i2=i2+1;
output(ic)=str2(i2);
end
end
ic=ic+1;
end
strc=char(output);
end
相似回答
大家正在搜