Code covered by the BSD License  

Highlights from
Combinations, n choose k

Be the first to rate this file! 7 Downloads (last 30 days) File Size: 1.26 KB File ID: #32626

Combinations, n choose k

by Bruce Raine

 

21 Aug 2011

This function finds the combinations of n choose k.

| Watch this File

File Information
Description

The combination function seems to be missing from MATLAB so this rendition fills the gap.

MYCOMBNK returns combinatorial of n choose k i.e all unique combinations of n elements taken k at a time.

EX: >> mycombnk(6,2)
 ans = 15

MATLAB release MATLAB 7.12 (2011a)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (4)
22 Aug 2011 Jan Simon

Have you seen the built-in function NCHOOSEK?! It uses a more stable method than your function, because factorial(n) cannot be represented exactly as DOUBLE for n>=19.

24 Aug 2011 mklcst mklcst

I really don't understand the purpose of this function.
It's simply a binomial coefficient, it would be better to create the possible combination with and w/o replacement.

24 Aug 2011 Bruce Raine

Actually I tested my function against NCHOOSEK and discovered it was fine until n=54 and then it differed only by a minuscule fraction of the mantissa. It seems stable above that too and incidentally NCHOOSEK becomes unstable at n=54. As for the FACTORIAL function it works well enough and in fact worked up to n=150 inside my function. I can send you my testbed code which shows how well the MYCOMBNK performs against MW function.

25 Aug 2011 Bruce Raine

The binomial coefficient is arguably the most important coefficient in probability. It is the basic building block of probability distribution functions, for example, the Binomial distribution and the Hypergeometric distribution. Indeed, the binomial coefficient sits at the heart ot Probabilistic Combinatorics.

As for replacement and w/o replacement, these two concepts depend on what kind of probability you intend to calculate and not the binomial coefficient. I have written several other functions making use of the binomial coefficient which I may upload soon.

Please login to add a comment or rating.
Tag Activity for this File
Tag Applied By Date/Time
combinatorics Bruce Raine 22 Aug 2011 09:39:46
statistics Bruce Raine 22 Aug 2011 09:39:46

Contact us at files@mathworks.com