5
$\begingroup$

I have a question how can I take coefficients of the variable from a matrix? for example; I have a matrix such as: A={{2a,3b,4c},{4d},{1a,1e}} variable is {a,b,c,d,e}

Now I would like to take all coefficients from that matrix to make a new matrix. Acoef={{2,3,4,0,0},{{0,0,0,0,4},{1,0,0,0,1}}

$\endgroup$

2 Answers 2

5
$\begingroup$
Total[D[A, {{a, b, c, d, e}}], {2}]
{{2, 3, 4, 0, 0}, {0, 0, 0, 4, 0}, {1, 0, 0, 0, 1}}

Also

Total[Transpose[Coefficient[A, #] & /@ {a, b, c, d, e}], {3}]
{{2, 3, 4, 0, 0}, {0, 0, 0, 4, 0}, {1, 0, 0, 0, 1}}

and

Transpose@Total[Coefficient[A, #] & /@ {a, b, c, d, e}, {3}]
{{2, 3, 4, 0, 0}, {0, 0, 0, 4, 0}, {1, 0, 0, 0, 1}}
$\endgroup$
1
  • $\begingroup$ Thank you so much! $\endgroup$
    – Vô Danh
    Feb 22, 2021 at 18:47
3
$\begingroup$

Another way.

A = {{2 a, 3 b, 4 c}, {4 d}, {a, e}};
(* Variables[A]=={a,b,c,d,e} *)
A /. Thread[{a, b, c, d, e} -> IdentityMatrix[5]]
% // Map[Total]

{{{2, 0, 0, 0, 0}, {0, 3, 0, 0, 0}, {0, 0, 4, 0, 0}}, {{0, 0, 0, 4, 0}}, {{1, 0, 0, 0, 0}, {0, 0, 0, 0, 1}}}

{{2, 3, 4, 0, 0}, {0, 0, 0, 4, 0}, {1, 0, 0, 0, 1}}

$\endgroup$
1
  • $\begingroup$ Thank you so much! $\endgroup$
    – Vô Danh
    Feb 22, 2021 at 18:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.