MATLAB: Extracting variables from symbolic expression in a matrix form

divisionmatrixmatrix manipulationsymbolicSymbolic Math Toolbox

I have a long equation in symbolic math toolbox which has the following form:
A*u1+B*u2+C*u3
A, B and C are long relationships consisting of symbolic variables. I would like to extract the A B C in from u1, u2 and u3 and write them in a matrix form. In other words I would like to shape a relationship such us
[A B C]*{u}
where u is
u=[u1;u2;u3]
How can I do that?

Best Answer

You could use coeffs but the output is a bit of a nuisance for multivariates.
You could call into MuPAD and use coeff.
But really, the easiest way is likely to use
A = subs(Expression, {u2,u3}, {0,0});
B = subs(Expression, {u1,u3}, {0,0});
C = subs(Expression, {u1,u2}, {0,0});
Related Question