How can I get only the coefficients that are multiplied by the powers of x? Example: 5x^2 + 9x + 12y + 3z >>> [5, 9]

1 view (last 30 days)
Hello
Let's say I have the following symbolic equation:
5x^2 + 9x + 5y + 3z
Is there a function that returns the coefficients that are multiplied by the powers of x.
Example:
SomeFunction(5x^2 + 9x + 12y + 3z , x) > [5 , 9]
SomeFunction(5x^2 + 9x + 12y + 3z , y) > [12]
SomeFunction(5x^2 + 9x + 12y + 3z , z) > [3]

Answers (2)

KSSV
KSSV on 24 Jul 2020

Steven Lord
Steven Lord on 24 Jul 2020
syms x y z
F = 5*x^2+9*x+5*y+3*z
[C, T] = coeffs(F, x)
C(logical(T ~= 1))
The logical call in the last line may look redundant, but it's necessary to convert (T ~= 1) from a symbolic expression to a logical expression.

Categories

Find more on Symbolic Math Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!