function bc = binomial(n,m) %BINOMIAL returns binomial coefficient % % BINOMIAL(n,m) reutrns the binomial coefficient % n choose m (given nonnegative integers n % and m with m <= n) % binomial.m by David Terr, Raytheon, 5-11-04 if length(m) > 1 for i=1:length(m) bc(i) = binomial(n,m(i)); end if size(m,1) > size(m,2), bc = bc'; end else if 2*m <= n m1 = m; else m1 = n-m; end if n <= 100 siz = abs( n-m1 ) + 1; ps = pascal( siz ); bc = ps( siz, m1+1 ); else % suggested by Greg von Winckel to improve speed for large input bc = exp( gammaln(n+1) - gammaln(n-m+1) - gammaln(m+1) ); end end