Hi I want to implement a recursive function which could replace the following code in Matlab:
p=0.2;n=10;a=5;p1=0;for i = 0:1:(n-a) for j = 0:1:(n-i-a) for k = 0:(n-i-j-a) for l = 0:(n-i-j-k-a) for m = 0:(n-i-j-k-l-a) p1=p1+(p*(1-p)^i)*(p*(1-p)^j)*(p*(1-p)^k)*(p*(1-p)^l)*(p*(1-p)^m); end end end endend
I could have used the above code if had a=5 or 10. But in my case, value of n is constant like n=100 and value of a can be up to 100, i.e, n>=a, which makes it difficult to change the number of for loops on each value of a. I will be thankful if someone helps me in implementing such a recursive function which could replace the above for loops.
Best Answer