求一个非全排列代码!(Matlab,C,...)

输入任意两个字符串,得到他们的排列存到一个数组中。这种排列是特殊的,满足如下规律,例如:将两个序列,(1,2,3)和(a,b,c,d)放在一起排列,但组内的顺序不变,即123的前后顺序不变,abcd的前后顺序不变,但两序列之间的元素可以互相穿插,最好是用matlab写的。别的也行。

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{ir,1}=char(output);
end
温馨提示:答案为网友推荐,仅供参考
第1个回答  2010-09-05
function output=fun(str1,str2)
%检验参数是否是字符串
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
r=0;
slen=ls1+ls2;
output=[];
i1=1;
i2=1;

for i=1:slen
if(i2<=ls2)r=randint;
else if i1<=ls1
r=1;
end
end
if r==1&&i1<=ls1
output(i)=str1(i1);
i1=i1+1;
else if i2<=ls2
output(i)=str2(i2);
i2=i2+1;
end
end
end
output=char(output);
相似回答
大家正在搜