loop executing more then the given iteration.

9 views (last 30 days)
i am calling one function inside the loop,in 1st iteration function calling and returning gud result,but when i have given more then one iteration(more then one function call),i am getting multiple result,loop should run only two time ,but its running three times.
it is because of function call inside the loop.
for y=1:count1 result(1)=comb(1); switch(y) case 1 for z=1:2 result(y+1)=acid(z); index=y+1; funcomb(index,result,acid); end end % switch end end
Any solution to this problem ?

Answers (3)

Amit
Amit on 27 Dec 2013
There are 2 loops. Loop y and loop z.
The only time switch is activating is for y = 1 ( I dont understand the purpose of that for y=1:count1 loop??) Inside the case 1, the loop is running twice however both times it is doing the same thing as they are are related to outer loop index, which will remain 1.
Can you post you results where you having trouble? That might help. And a little explanation of the purpose - I am only asking because it seems from this code that the loops are unnecessary here.

Uttam kumar
Uttam kumar on 28 Dec 2013
Hi Amit, Thanks for your reply. Actually,i am trying to find all possible combination form the given expression L/M-A-A-A-A-A-Y-A-A-A-A-A-K/N
And each A can be [A,B,C,D,E]
so from the above expression ,thousands of combination possible.
some output results:- LAYAK LAYBK LAYCK LAYDK LAYEK LAYAAK LAYABK LAYACK ..... ..... LAYAAAK LAYAABK LAYAACK LAYAADK ...... ...... LAYAAAAK LAYAAABK LAYAAACK ....... ....... LAYAAAAAK LAYAAAABK LAYAAAACK ......... LAYEEEEEK
Like this ,i want all combination with left side A,s
i have written one function which will give all combinations of A,s after 'Y' And same function i want to call every time for all A,s which is present before 'Y'.
here is my code. comb=['L','A','A','A','A','A','Y','A','A','A','A','A','k']; result=[]; acid=['A','B','C','D','E']; count1=length(acid);
%function to find the all possible combination
function funcomb(index,result,acid) result(index+1)='Y'; %count2=1; 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)='K'; fprintf('%s',strvcat(result)); fprintf("\n"); end
case 2 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)='K'; fprintf('%s',strvcat(result)); fprintf("\n"); end end case 3 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)='K'; fprintf('%s',strvcat(result)); fprintf("\n"); end end end case 4 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)='K'; fprintf('%s',strvcat(result)); fprintf("\n"); end end end end case 5 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)='K'; fprintf('%s',strvcat(result)); fprintf("\n"); end end end end end end %switch end %count2 ++; end end
% possible combination function end
for y=1:count1 result(1)=comb(1); switch(y) case 1 for z=1:length(acid) result(y+1)=acid(z); index=y+1; funcomb(index,result,acid); fprintf("loop"); end end % switch end end
so the 'funcomb'function is a quite heavy .but whenever i am calling this function more then once ,i am getting same result sequence more then the loop iteration.
for example if iteration is 2 for z=1:2 result(y+1)=acid(z); index=y+1; funcomb(index,result,acid); the output is giving
LAYAK ..... ..... LAYAAAAAK LBYAK ..... LBYDBCADK (its not printing upto LBYEEEEEK) LAYAK ..... ..... LAYAAAAAK LBYAK ..... LBYDBCADK LAYAK ..... ..... LAYAAAAAK LBYAK ..... LBYDBCADK
same results three times. i have observed this is happening when the functions calls more then one time.
i hope i have explained well.
Any better idea to handle this issue in matlab?

Amit
Amit on 28 Dec 2013
Edited: Amit on 28 Dec 2013
This might be a little help for what you trying to do.
To be honest, I am a bit confused looking at your code. I am trying to justify the for loop along with switch? The switch only evaluates the first time in the loop and does not do anything afterwards.
Also, am I correct in saying that from a [L/M-A-A-A-A-A-Y-A-A-A-A-A-K/N] expression, with A = [A B C D E], you want sequences where the second term is A and the 3 term is Y and last term is K? [LAYAK], [LAYABK] etc etc?

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!