Binomial coefficients with Matla

edited March 2009 in Science & Tech
I need to compute binomial coefficients recursively with Matlab, like a choose k=a!/k!(a-k)! Below is what I have written to first compute the factorial and then to compute the coefficients using the factorial I wrote, but it does not work. Any help?


function fact=recfact(n)
n==1
fact=1;
fact=n*recFact(n-1);
end
function coef=coef(a,k)
coef=recfact(a)/(recfact(k)*recfact(a-k))
end

Comments

  • drasnordrasnor Starship Operator Hawthorne, CA Icrontian
    edited March 2009
    First, please don't change the font colors and faces when you post. If you need to block out your program use the code /code BB tags.

    The statement n==1 in function recfact(n) does nothing. Since your recursion has no criteria for ending it will keep going forever.

    -drasnor :fold:
  • edited March 2009
    Thanks, but ok, so I tried making that n==1 into an "if", but it still does not work.
  • shwaipshwaip bluffin' with my muffin Icrontian
    edited March 2009
    use the debugger and see why it's not working.
Sign In or Register to comment.