# Error using Solve on Matrix Multiplication

I am trying to make a function the will find that value of $x$ in the following equation: $x.a=b$

all values of $x, a, b$ are matrices. The dimensions of $x$ are unknown, but $a$ and $b$ have the same dimensions of $n*8$ where $n$ depends on the matrix used in the function

I am to understand that the dimensions of $x$ are going to be $n*n$ so that the shapes of the matrix are compatible in the equation.

The functions arguments are simply $a$ and $b$, and I tried setting it up as follows:

GetValueOfX[a_,b_] := Solve[x.a==b,x]


But when I try put in values for $a$ and $b$, I get the following error:

Solve::nsmet : This system cannot be solved with the methods available to Solve.


I am very confused by this, is it telling me there are an infinite number of outcomes (I googled the error and this was a common cause) or is this kind of Matrix Multiplication incompatible with Solve as it does not know the dimensions of $x$. And more importantly, is there always going to be an answer?

The values I tested were:

$a=\begin{matrix}116&101&115&116&105&110&103&44\\32&116&101&115&116&105&110&103\end{matrix}$

$b=\begin{matrix}114&117&110&110&105&110&103&44\\32&114&117&110&110&105&110&103\end{matrix}$

P.S. if you want to know, those matrices are the character codes of a phrase and that is how I got them.

If I understand you correctly, you are trying to solve $A x=b$ for $x$.

The dimensions of $x$ is not unknown, clearly it is known, since the inner dimensions must match between $A$ and $x$ and the outer dimension must match with $b$. Any way, may be you can look at LinearSolve

n = 2;
a = Table[RandomReal[1], {n}, {8}];
b = Table[RandomReal[1], {n}, {8}];
(x = LinearSolve[a, b]) // MatrixForm


The above does mldivide which is in Matlab written as A\B, to do mrdivide which is B/A, then these are related by B/A=(A'\B')' hence

n = 2;
a = Table[RandomReal[1], {n}, {8}];
b = Table[RandomReal[1], {n}, {8}];
x = ConjugateTranspose[LeastSquares[Transpose[a], Transpose[b]]]


Dimensions matches ok. xA=B since $A$ is $2 \times 8$ and $B$ is $2 \times 8$ then $x$ is $2 \times 2$

• but how can i do $x.a=b$, this only shows $a.x=b$ and matrix multiplication is not commutative so it isn't the same – VikeStep Aug 16 '13 at 7:21
• @user9053 so, you are then trying to do mrdivide and not mldivide? if so, please see update. – Nasser Aug 16 '13 at 7:43
• I tried using the updated mrdivide bit, but when substituting it back into the equation $x.a=b$, I got a different result for $b$ so it doesn't work – VikeStep Aug 16 '13 at 8:10
• @user9053 it is using least squares solution. We are using random numbers. But you have above Ax=b and xA=B definitions that I know about. Well, this is the limit of my linear algebra skills. If I think of something else will add. – Nasser Aug 16 '13 at 8:12
• Thanks for your help, hopefully someone else finds a solution. When putting //N at the end of the equation however, the final answer was about 0-10 off each value, so its close but no cigar – VikeStep Aug 16 '13 at 8:16

Your no of unknown = n^2; Your no of equation = 8n;

So you can only get a unique solution for your system when n^2=8n. So only for n=8 its possible to get a unique solution.

This is just to show that your system of equation is not solvable for n=2(or any other integer excluding 8)

n = 2;
a = {{116, 101, 115, 116, 105, 110, 103, 44}, {32, 116, 101, 115, 116,
105, 110, 103}};
b = {{114, 117, 110, 110, 105, 110, 103, 44}, {32, 114, 117, 110, 110,
105, 110, 103}};


(* Create array containing elements of the square matrix x and the array eq for L.H.S of x.a=b *)

 Array[x, {n, n}]; Array[eq, {n,8}];


(* index list for summing up in the matrix multiplication*)

 list = Table[{j}, {j, 1, n}];


(* List of index of all elements in x *)

 slist = Partition[Flatten[Table[{i, j}, {i, 1, n}, {j, 1, n}]], 2];
varList = x[#[[1]], #[[2]]] & /@ slist


(* Equates the ij th element of L.H.S to R.H.S*)

 eq[i_, j_] :=
Module[{},
b[[i, j]] - Total[x[i, #[[1]]]*a[[#[[1]], j]] & /@ list] == 0]


(* index list for all the n * 8 equations*)

list1 = Partition[Flatten[Table[{i, j}, {i, 1, n}, {j, 1, 8}]], 2];
eqList = eq[#[[1]], #[[2]]] & /@ list1(* list of all equations*)


The output gives you 16 equations.

partialsolutionPossible[n1_, n2_, n3_, n4_] :=
Solve[{eqList[[n1]], eqList[[n2]], eqList[[n3]], eqList[[n4]]},
varList]
(* This gives the possible solutions for four equations chosen at a
time n1 and n2 has to be between 1 & 8 n3 and n4 is between 9 & 16 *)

partialsolutionPossible[1, 2, 9, 10]
(* This is not the solution for  the fullsystem because you cannot
simultaneously satisfy all 16 together*)


{{x[1, 1] -> 395/426, x[1, 2] -> 343/1704, x[2, 1] -> 4/639, x[2, 2] -> 1249/1278}}

• why is that, are you saying that if n is 3 that there are multiple solutions for the 3*3 matrix which is $x$ Keep in mind that a 3*3 matrix by a 3*8 matrix returns a 3*8 matrix. – VikeStep Aug 16 '13 at 9:35
• If x is 3*3 matrix then it has 9 elements.So solving x implies finding the value of those 9 unknowns.So for n=3 x.a will give 3*8 matrix containing 24 elements with where each one of them is some linear combination of those 9 unknowns.Now you equate these 24 equations to the corresponding one on R.H.S.Basically u will end up with 24 equation in 9 unknowns which is called an overdetermined system. link – Hubble07 Aug 16 '13 at 9:59
• ok, so lets say I want to stick to this overdetermined system, how do I find one of the possible solutions that exist? All I need is one example for what I am trying to achieve, or are you saying that there is no solution at all? – VikeStep Aug 16 '13 at 10:17
• Yes im saying that there is no solution in that case.have a look at the case for n=2. – Hubble07 Aug 16 '13 at 13:48
• Thanks, I'll have to try and make $a$ and $b$ square matrices for this to work then – VikeStep Aug 16 '13 at 22:01