Math Matlab

Calculating the indices for n choose k

So if you want to calculate n choose k, or n over k or all k different combinations of n unique items or binomial coefficients in Matlab or in math language

\[\frac{n!}{(n-k)! k!}\]

you can write

nchoosek(n,k)

But lets say you want to generate a list of these possible combinations? Well then you just write n as a vector

n = [1, 2, 3, 4] = [1:4]

so this would generate using $$k = 2$$

nchoosek(1:4,2)[1 21 31 42 32 43 4]

or why not do

nchoosek([1,4,10,17],2) = [1 41 101 174 104 1710 17]

Awesome discovery at 8.00 pm!

Leave a Reply

Your email address will not be published. Required fields are marked *