How do I display the terms of a binomial expansion (x+y)^n ?

7 views (last 30 days)
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

Accepted Answer

Matt J
Matt J on 19 Jun 2019
Edited: Matt J on 19 Jun 2019
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 '
  3 Comments
Matt J
Matt J on 19 Jun 2019
Just call sprintf() differently
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 '

Sign in to comment.

More Answers (0)

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Products


Release

R2013a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!