MATLAB: Isolate Coefficients of Multivariate Linear Polynomial

coeffsMATLABmultivariatepolynomialsyms

Hi, I have a symbolic expression
eqn = r - 2*x +7/4
I would like to isolate the coefficients of the variables (x, r and y (y's coefficient is 0)). How do I do that? I've tried using coeffs but that only lets me specify one variable. It would also be ok if I got two vectors back:
[ -2 , 1 , 7/4]
[x , r , 1]
even if they don't include y. Thanks!

Best Answer

If you know that you have only linear terms and only x, r, and y, you could use diff to get the coefficients:
syms x r y
eqn = r - 2*x +7/4;
[diff(eqn, x), diff(eqn, r), diff(eqn, y)]
This gives:
ans =
[ -2, 1, 0]