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)
Show older comments
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]
0 Comments
Answers (2)
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.
0 Comments
See Also
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!