MATLAB: How to display the terms of a binomial expansion (x+y)^n

binomial expansionMATLAB

I want to display the terms in terms of x and y i.e nCr x^(n-r) y^r with r going from 0 to n

Best Answer

One way,
n=4;
e=0:n;
C=arrayfun(@(i)nchoosek(n,i),e);
sprintf('%d*x^%d*y^%d ',[C;e;n-e])
'1*x^0*y^4 4*x^1*y^3 6*x^2*y^2 4*x^3*y^1 1*x^4*y^0 '