# Solve for matrix in linear equation

I need to find a matrix M such that M.u = v, where  u and v are known vectors. I don't understand how to do this with LinearSolve, which seems only to solve for the vectors and not the matrices in these types of equation.

Is there a function for this?

There are too many matrixs satisfy the equation M.u==v.

Without loss general, we can Normalize the vector u and v.

Besides the RotationMatrix which have mention by @Daniel Huber, we can construct the reflect-matrix by hand which also satisfiy the equation M.u==v

u = Normalize[{x, y, z}, Sqrt[#.#] &];
v = Normalize[{a, b, c}, Sqrt[#.#] &];
normal = Normalize[u - v, Sqrt[#.#] &];
M = IdentityMatrix - 2 Outer[Times, normal, normal];
M.u == v // Simplify


True

ReflectionMatrix maybe another choise.

u = Normalize[{x, y, z}, Sqrt[#.#] &];
v = Normalize[{a, b, c}, Sqrt[#.#] &];
normal = Normalize[u - v, Sqrt[#.#] &];
M = Simplify[ReflectionMatrix[normal], normal ∈ Reals];
M.u == v // Simplify


True

For difference dimensions vector u and v,we can also combine the matrixs such as projection matirx and reflection matrix and rotation matirx to construct such matrix.

• Excellent solution, thanks! It's wonderful that this question got three separate answers, all embodying different ideas, and all of which work. Nov 9, 2020 at 19:07

You can use a fitting procedure to find a solution. It's not going to be unique, though.

u = RandomReal[1, {3}]
v = RandomReal[1, {5}]
mat = Array[x, {Length[v], Length[u]}]
sol = NMinimize[Total[(mat.u - v)^2], Flatten[mat]];
mat /. Last[sol]


Check that the residuals are 0:

mat.u - v /. Last[sol]


You may search for a rotation matrix that turns the direction of u into the direction of v. Applying this matrix to u gives a vector in the direction of v, but with a different length. Therefore you must adjust the rotation matrix by multiplying by the length of v and divide by the length of u. THis can be done in MMA by:

{u, v} = RandomReal[{-1, 1}, {2, 3}]
m = RotationMatrix[{u, v}]  Norm[v]/Norm[u]


m maps now u into v:

m.u == v
(*True*)
{"ft_lang_label":"__label__en","ft_lang_prob":0.7350517,"math_prob":0.9973243,"size":868,"snap":"2023-40-2023-50","text_gpt3_token_len":256,"char_repetition_ratio":0.17592593,"word_repetition_ratio":0.30882353,"special_character_ratio":0.3076037,"punctuation_ratio":0.23595506,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9996363,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-10-03T13:57:16Z\",\"WARC-Record-ID\":\"<urn:uuid:f83daac0-9bc9-4d28-93aa-8910f3690b2a>\",\"Content-Length\":\"176552\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:7a52473f-c411-43c0-a245-655bdd2a60d6>\",\"WARC-Concurrent-To\":\"<urn:uuid:d543235d-ff0a-491d-b0f7-c8336cbed4f4>\",\"WARC-IP-Address\":\"104.18.11.86\",\"WARC-Target-URI\":\"https://mathematica.stackexchange.com/questions/234077/solve-for-matrix-in-linear-equation\",\"WARC-Payload-Digest\":\"sha1:T2KILHSLJHYW45V33KBTK3K7BOS7S7C4\",\"WARC-Block-Digest\":\"sha1:Q5KZP45MOAWBNSGMKSOHXHC7M6S6J7Y4\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-40/CC-MAIN-2023-40_segments_1695233511106.1_warc_CC-MAIN-20231003124522-20231003154522-00421.warc.gz\"}"}