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

1 visualización (últimos 30 días)
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]

Respuestas (2)

KSSV
KSSV el 24 de Jul. de 2020

Steven Lord
Steven Lord el 24 de Jul. de 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.

Categorías

Más información sobre Symbolic Math Toolbox en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by