Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am wondering how to change the output format in Mathematica. For example, I have $x=\binom{3}{1}$ and $y=\binom{2}{5}$, and I want to find what linear combination of $x$ and $y$ produces $\binom{7}{11}$. What do I have to do to make Mathematica display the output $x+2y$ ?

I tried using Solve, but Mathematica just returns the coefficients. I also tried to use the augmented matrix, but again Mathematica still did not give me the output I want.

P.S. I am working with a 16 by 16 matrix with 16 variables, so it's not very convenient to read off the coefficients for each variable.

share|improve this question
possible duplicate of General form of a linear transformation – Artes Jun 13 '14 at 10:44
up vote 6 down vote accepted
x = {3, 1}
y = {2, 5}
a  Defer[x] + b Defer[y] /. First@Solve[a x + b y == {7, 11}, {a, b}]

(* x + 2 y *)

Note that the output is usable as such: evaluate it and you'll get the combination result.

If only integer coefficients are desired, changing the Solve to something like:

Solve[a x + b y == {7, 11} && {a, b} \[Element] Integers, {a, b}]

will accomplish this. If you extend things where no solutions, or multiple solutions are possible, probably want to do the Solve first, check it, then do the replace.

Edit: Here's a sketch (meaning not heavily tested, surely more elegant ways to do same) of how you might generalize this into a function:


SetAttributes[combo, HoldAll]l

combo[a_, b_, c_] := 
 Block[{d = Defer /@ Unevaluated[{a, b}], syms = Table[Unique[], {2}]},
  Total[syms*d] /. First@Solve[Total[{a, b}*(syms)] == c, syms]];

(* cobble up an example *)

m1 = {{1, 2}, {3, 4}};
m2 = {{3, 5}, {7, 9}};

(* do some combo *)
result = 3 m1 - 2 m2

(* solve and output as desired *)
combo[m1, m2, result]

(* 3 m1 - 2 m2 *)

As above, the result can be used/evaluated. If you go the generalized function route, you probably do want to check for no-solution cases, add desired limitation(s) to solve (e.g. integer only), and perhaps extend it to more than two + result arguments...

share|improve this answer
Wow that is quick. Thank you very much. – snowball Jun 13 '14 at 9:32

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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