# Solve a set of equations in matrix form

I am trying to solve a set of equations in the form ui+vj=cij up until now I have done a rather complicated work around which is not entirely satisfactory.

What I would like to do is to use the vectors u and v inside the Solve command instead of the ugly solution I've come up with. I want to be able to write something like u[[i]]+v[[j]]==c[[i,j]]

Is is possible?

c = {{36, 32, 33, 19}, {10, 8, 7, 20}, {12, 17, 16, 29}, {23, 15, 16,
28}} // MatrixForm

uivj = Solve[{ 0 + v1 == c[[1, 1, 1]],
0 + v4 == c[[1, 1, 4]],
u2 + v1 == c[[1, 2, 1]],
u2 + v2 == c[[1, 2, 2]],
u2 + v3 == c[[1, 2, 3]],
u3 + v1 == c[[1, 3, 1]],
u4 + v2 == c[[1, 4, 2]]}, {u2, u3, u4, v1, v2, v3, v4}]

u = {{0}, {uivj[[1, 1, 2]]}, {uivj[[1, 2, 2]]}, {uivj[[1, 3, 2]]}}
v = {{uivj[[1, 4, 2]], uivj[[1, 5, 2]], uivj[[1, 6, 2]],
uivj[[1, 7, 2]]}}


• Don't use MatrixForm. It is a display wrapper and interferes with evaluation. – Roman Feb 15 at 15:54
• It get's a little bit easier, but the main problem remains. I want to be able to write something like u[[i]]+v[[j]]==c[[i,j]] – Lisch64 Feb 15 at 16:02

## 1 Answer

You can use Table:

c = {{36, 32, 33, 19}, {10, 8, 7, 20}, {12, 17, 16, 29}, {23, 15, 16, 28}};

eq = Table[Subscript[u, i] + Subscript[v, j] == c[[i, j]], {i, 1, 4}, {j, 1, 4}]

uivj = Solve[
Flatten[eq], {Subscript[v, 1], Subscript[v, 2], Subscript[v, 3],
Subscript[v, 4], Subscript[u, 1], Subscript[u, 2], Subscript[u, 3],
Subscript[u, 4]}]


Plain code may look complicated, this is the image of the code: Use Ctrl - to type subscript $$i$$ , $$j$$ , ...

Update:

As CA Trevillian mentioned, using subscript is not the best practice, so the following code is a better version to use and easier to change:

eq = Table[u[i] + v[j] == c[[i, j]], {i, 1, 4}, {j, 1, 4}];

Solve[Flatten[eq], Flatten[Table[{u[i], v[i]}, {i, 1, 4}]]]

• This solution is very elegant, also generic for any matrix size! Thanks a lot! – Lisch64 Feb 15 at 18:32
• It’s generally not advised to use subscripts. I’d recommend using something like a Format wrapper around v[j] and u[i]. – CA Trevillian Feb 15 at 19:05