In an attempt to vectorize a particular piece of Matlab code, I could not find a straightforward function to generate a list of the binomial coefficients. The best I could find was nchoosek
, but for some inexplicable reason this function only accepts integers (not vectors of integers). My current solution looks like this:
mybinom = @(n) arrayfun(@nchoosek, n*ones(1,n), 1:n)
This generates the set of binomial coefficients for a given value of n
. However, since the binomial coefficients are always symmetric, I know that I am doing twice as much work as necessary. I'm sure that I could create a solution that exploits the symmetry, but I'm sure that it would be at the expense of readability.
Is there a more elegant solution than this, perhaps using a Matlab function that I am not aware of? Note that I am not interested in using the symbolic toolbox.