File Exchange

image thumbnail

binomial.m

version 1.0.0.0 (394 Bytes) by David Terr
This program computes the binomial coefficient C(n,m).

1 Download

Updated 04 Jun 2004

No License

Given nonnegative integers m and n with m <= n, this program computes the binomial coefficient C(n,m). It's about time someone wrote code to do this! I couldn't find it anywhere.

Cite As

David Terr (2021). binomial.m (https://www.mathworks.com/matlabcentral/fileexchange/4901-binomial-m), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (5)

John D'Errico

Sorry. I think Herbert completely misunderstands the issue. In fact, he seems to be wrong on all counts.

It DOES have an overflow issue, just as as nchoosek has an issue. For example,

>> nchoosek(1500,265)
Warning: Result may not be exact. Coefficient is greater than 9.007199e+15 and is only accurate to 15 digits
> In nchoosek at 92
ans =
1.58269376513703e+302

nchoosek does return a valid answer, WITH a warning that it is not exact. But since the result has 303 decimal digits and is returned as a double what do you expect???

binomial also returns the same thing, however it generates no warning message. That does not say it has no issue, only that it fails to be friendly and warn you of that fact!

Both codes will return inf when the arguments cause a double to be exceeded.

binomial(1500,275)
ans =
Inf

However, note that nchoosek is both more flexible and more powerful than is binomial. And nchoosek has been around since the dark ages. So the fact that David could not find it is merely a reflection that he did not look that hard. Jos points out the simple use of lookfor that would have given him the answer.

nchoosek also has the ability to give exact results for a slightly larger set of inputs than binomial. So if the input is uint64, so will be the output.

>> b1 = nchoosek(uint64(65),30)
b1 =
3009106305270645216

>> b2 = binomial(65,30)
b2 =
3.00910630527064e+18

>> b1 - b2
ans =
480

Here nchoosek was exact, whereas binomial was wrong. In fact, nchoosek CAN do symbolic computation, whereas binomial fails to return anything of value.

>> b1 = nchoosek(sym(1500),750)
b1 =
722462892448930217028116073228485295944376343040546523665632913653613596406381727723198055046187955623124069093412562720869577867557610868874271130828359513282184417776042792372322074253393127328396528996120053749558122610911178218582669317535346728464707661495135518682519172221470420360910320792434869988224466647627642393919250205687942318888922893189087379790541907686956429837978631252775258630376332505697937034877619012586751274240109457111424

>> binomial(sym(1500),30)
ans =
exp(gammaln(1501) - gammaln(1471) - 5253606334386405/70368744177664)

Of course, nchoosek even works perfectly for vpi input, whereas binomial fails there completely.

There is only one point made by Herbert that MIGHT be correct in some circumstances, i.e., that binomial is faster. Since binomial does no error checking and it has lesser capability than does nchoosek, of course it might be faster. On top of that, the gamma log computation for large input might be be pretty fast. But is it good? Even for double inputs...

>> binomial(101,10)
ans =
19212541264839.3

>> nchoosek(101,10)
ans =
19212541264840

Oops. I'm pretty sure that most binomial coefficients are integers, at least they were when I went to school. You never know with the new math though. But I would think it disappointing to see a floating point result that is not a pure integer, at least for a reasonably small set of inputs.

So how about speed. Is it faster? I'll use timeit to do the comparison.

>> timeit(@() nchoosek(100,10))
ans =
8.32913416666667e-05

>> timeit(@() binomial(100,10))
ans =
0.0002867962125

So here nchoosek was actually 4 times FASTER than binomial. Not slower. binomial is the slow one.

Herbert

perfect, it's faster than nchoosek and doesn't seem to have an overflow issue.

Mark Pijnenburg

Indeed this function does not work for symbolic computations so should be removed from this list. It works for numerical computations but for this there are better alternatives, e.g. nchoosek or other.

B Hill

nchoosek works fine. This function only appears to support numeric calculation. Remove post from "Symoblic Math" section as it does not belong here.

Jos vdG

lookfor "binomial coefficient" returns
NCHOOSEK

MATLAB Release Compatibility
Created with R12
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!