need to find all combination from the expression

8 次查看(过去 30 天)
All possible combination of chars.
Expression=(l/v-x(1,5)-y-x(1,5)-k/r)
x(1,5) means 1 to 5 x combination
Example:-
x
xx
xxx
xxxx
xxxxx
Final output Example :-
LXYXK
LXYXXK
LAYXXXXK
LAYXXXXXK
LXXYXK
LXXXYXXK
......

回答(2 个)

Uttam kumar
Uttam kumar 2013-12-11
Any better way to do this? Please provide your comments
************************************************************************************ array1=['L';'V'] array2=['A';'A';'A';'A';'A'] array3='Y' array4=['K';'R']
for M=1:length(array4)
for i=1:length(array1) cnt=1 cnt1=1 for j=1:length(array2)*length(array2)
if cnt > length(array2)
if cnt1 < length(array2)
cnt1=cnt1+1
end
cnt=1
end
fprintf('%s',array1(i))
for k=1:cnt1
fprintf('%s',array2(k))
end
fprintf('%s',array3)
for L=1:cnt
fprintf('%s',array2(L))
end
fprintf('%s\n',array4(M))
if cnt <= length(array2)
cnt=cnt+1
end
end
end
end
************************************************************************
output:-
cnt = 1
cnt1 = 1
LAYAK
cnt = 2
LAYAAK
cnt = 3
LAYAAAK
cnt = 4
LAYAAAAK
cnt = 5
LAYAAAAAK
cnt = 6
cnt1 = 2
cnt = 1
LAAYAK
cnt = 2
LAAYAAK
cnt = 3
LAAYAAAK
cnt = 4
LAAYAAAAK
cnt = 5
LAAYAAAAAK
cnt = 6
cnt1 = 3
.........................
how to hide the 'cnt' and 'cnt1' variable value display in output.

Uttam kumar
Uttam kumar 2014-3-18
result={13}; acid={'Ala','Arg','Asp','Asn','Cys','Gln','Glu','Gly','His','Ile','Leu','Lys','Met','Phe','Pro','Ser','Thr','Trp','Tyr','Val'}; %acid={'Ala','Arg','Asp','Asn','Cys'}; %acid=['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T']; ends={'L','V','K','R'}; lIndex=1;
%function to find the all possible combination
function funcomb(index,result,acid,last,fileID) result(index+1)='Y';
for count2=1:length(acid)
switch (count2)
case 1 for p=1:length(acid) result{index+1+1}=acid{p}; result{index+1+1+1}=last; %fprintf('%s\t',strvcat(result)); fprintf(fileID,'%s',result{:}); fprintf(fileID,"\n");
end
case 2 %fprintf(fileID,"\n"); for p=1:length(acid) result{index+1+1}=acid{p}; for q=1:length(acid) result{index+1+1+1}=acid{q}; result{index+1+1+1+1}=last; %fwrite(fileID,'%s\t',result); fprintf(fileID,'%s',result{:}); fprintf(fileID,"\n"); end end
case 3 %fprintf(fileID,"\n"); for p=1:length(acid) result{index+1+1}=acid{p}; for q=1:length(acid) result{index+1+1+1}=acid{q}; for r=1:length(acid) result{index+1+1+1+1}=acid{r}; result{index+1+1+1+1+1}=last; %fwrite(fileID,'%s\t',result); fprintf(fileID,'%s',result{:}); fprintf(fileID,"\n"); end end end
case 4 %fprintf(fileID,"\n"); for p=1:length(acid) result{index+1+1}=acid{p}; for q=1:length(acid) result{index+1+1+1}=acid{q}; for r=1:length(acid) result{index+1+1+1+1}=acid{r}; for s=1:length(acid) result{index+1+1+1+1+1}=acid{s}; result{index+1+1+1+1+1+1}=last; %fwrite(fileID,'%s\t',result); fprintf(fileID,'%s',result{:}); fprintf(fileID,"\n"); end end end end
case 5 %fprintf(fileID,"\n"); for p=1:length(acid) result{index+1+1}=acid{p}; for q=1:length(acid) result{index+1+1+1}=acid{q}; for r=1:length(acid) result{index+1+1+1+1}=acid{r}; for s=1:length(acid) result{index+1+1+1+1+1}=acid{s}; for t=1:length(acid) result{index+1+1+1+1+1+1}=acid{t}; result{index+1+1+1+1+1+1+1}=last; %fwrite(fileID,'%s\t',result); fprintf(fileID,'%s',result{:}); fprintf(fileID,"\n"); end end end end end end %switch end end
end
% possible combination function end
fileID = fopen('Combfile.txt','wt'); for a=1:2 for z=3:4 last=ends{z};
for y=1:5 result{1}=ends{a}; switch(y)
case 1 for p=1:length(acid) result{lIndex+1}=acid{p}; rIndex=lIndex+1; funcomb(rIndex,result,acid,last,fileID); end
case 2 for p=1:length(acid) result{lIndex+1}=acid{p}; for q=1:length(acid) result{lIndex+1+1}=acid{q}; rIndex=lIndex+1+1; funcomb(rIndex,result,acid,last,fileID); end end
case 3 for p=1:length(acid) result{lIndex+1}=acid{p}; for q=1:length(acid) result{lIndex+1+1}=acid{q}; for r=1:length(acid) result{lIndex+1+1+1}=acid{r}; rIndex=lIndex+1+1+1; funcomb(rIndex,result,acid,last,fileID); end end end
case 4 for p=1:length(acid) result{lIndex+1}=acid{p}; for q=1:length(acid) result{lIndex+1+1}=acid{q}; for r=1:length(acid) result{lIndex+1+1+1}=acid{r}; for s=1:length(acid) result{lIndex+1+1+1+1}=acid{s}; rIndex=lIndex+1+1+1+1; funcomb(rIndex,result,acid,last,fileID); end end end end
case 5 for p=1:length(acid) result{lIndex+1}=acid{p}; for q=1:length(acid) result{lIndex+1+1}=acid{q}; for r=1:length(acid) result{lIndex+1+1+1}=acid{r}; for s=1:length(acid) result{lIndex+1+1+1+1}=acid{s}; for t=1:length(acid) result{lIndex+1+1+1+1+1}=acid{t}; rIndex=lIndex+1+1+1+1+1; funcomb(rIndex,result,acid,last,fileID); end end end end end end % switch end end end end status=fclose(fileID); status
This code is running continuously like never ending .Can anyone suggest me ,how to optimize the number of for loops.
Thanks uttam

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by