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 2; 1 3; 1 4; 2 3; 2 4; 3 4]

or why not do

nchoosek([1,4,10,17],2) = [1 4; 1 10; 1 17; 4 10; 4 17; 10 17]

Awesome discovery at 8.00 pm!

Leave a Reply

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