I'd like to implemente a matrix multiplication among three matrices sized at 1*256, 256*256 and 256*1. But I'm stucked at matrices type setting and multiplication. Currently my code is like

Inner[Times, Table[N[XSub[10, r]], {1}, {r, 1, 256}], MatrixForm[A], Table[N[YSub[10, s]], {s, 1, 256}, {1}]]

where A sized at 256*256. The result I expected is like enter image description here

Two Questions:

  1. I'm confused at creating the 1D matrix by Table[] , List[] or something else. My code above can create the Matrices I want, but it dosen't work on Matrix multiplication I wanted.
  2. I've tried to combine . and * to reach expected result on small-size matrices. but it dosen't work on matrices I created above.
  • 1
    $\begingroup$ I'm confused... if you multiply matrices of sizes 1x256, 256x256, 256x1 then the answer is a scalar, not a matrix. $\endgroup$ – bill s Jul 4 at 21:09
  • 4
    $\begingroup$ MatrixForm is for display only. It kills a computation when it appears inside one. $\endgroup$ – m_goldberg Jul 4 at 21:26
  • 1
    $\begingroup$ One should know that a vector has not to be specified as column or row in WL. $\endgroup$ – Αλέξανδρος Ζεγγ Jul 5 at 11:16
  • 1
    $\begingroup$ Hi @bills, a scalar and a 1-by-1 matrix are different, strictly speaking. $\endgroup$ – Αλέξανδρος Ζεγγ Jul 5 at 11:18

Matrix operations, especially with vectors, are a bit confusing in Mathematica.

ClearAll[v1, v2, a, b, c, x, y, z];
v1 = {a, b, c}; m = Partition[Range[9], 3]; v2 = {x, y, z};

Use MatrixForm to display the expressions nicely. They are still just lists.

Map[MatrixForm, {v1, m, v2}];

Then the inner product or Dot gives a scalar as Bill said (1x3, 3x3, 3x1)


    (a + 4 b + 7 c) x + (2 a + 5 b + 8 c) y + (3 a + 6 b + 9 c) z

What you might mean is a "3BY1 1BY3" matrix multiplication

z1 = KroneckerProduct[v1, v2]

    {{a x, a y, a z}, {b x, b y, b z}, {c x, c y, c z}}

This next operation (with *) is not really a normal kind of matrix or tensor operation, so check your mathematics and your meaning, but you can do it.

z2 = z1 * m

    {{a x, 2 a y, 3 a z}, {4 b x, 5 b y, 6 b z}, {7 c x, 8 c y, 9 c z}}

Gives what you seek. Realize there are three kinds of operation used here: Dot/Inner, KroneckerProduct (see also TensorProduct and Outer), and Times.

Hope this helps.

(All--I'll take any kind of guidance or link in the chat room on how to better format answers and show 2D output in StackExchange.)

| improve this answer | |
  • $\begingroup$ Thanks for your detailed explaination, it answered my question. But my 256*256 size matrix still dose not work. Reason might be that matrix cannot be multiplied here. For example, in your answer, m is a list. But my matrix here is not list type. And the computation result is just putting them together. How can I solve it? $\endgroup$ – PalvinWang Jul 4 at 23:34
  • $\begingroup$ Uh, I got it. I just deleted the MatrixForm from initial input. Thanks for all response! $\endgroup$ – PalvinWang Jul 5 at 1:20
  • 1
    $\begingroup$ Yep. You rarely (very rarely) want to mix Set(=) or SetDelayed(:=) with MatrixForm or similar commands. I've been burned so many times with a = (SOME OPERATION) // MatrixForm. $\endgroup$ – PaulCommentary Jul 5 at 3:47
  • $\begingroup$ (+1) Very nice! Also Outer[Times, v1,v2] m1 and (it would seem) (TensorProduct[v1,v2] m1)==(KroneckerProduct[v1,v2] m1)==Inner[Times, (v1 m1), v2, List]==(Outer[Times, v1,v2] m1) (with v1,v2 and m1 defined in my own effort). $\endgroup$ – user1066 Jul 5 at 11:09
  • $\begingroup$ @PaulCommentary use the grave key ` to type commands, e.g. "grave"Map"grave" -> Map $\endgroup$ – Kai Jul 5 at 15:41
Inner[Times, (v1 m1), v2, List]

{{a x, 2 a y, 3 a z}, {4 b x, 5 b y, 6 b z}, {7 c x, 8 c y, 9 c z}}


List@@@((m1 v1).v2)


Outer[Times, v1,v2] m1


v1= {a,b,c}
m1= {{1,2,3},{4,5,6},{7,8,9}};


Combining with the answer of PaulCommentary:

Inner[Times,(m1 v1), v2, List]==
List@@@((m1 v1).v2)==
(Outer[Times, v1,v2] m1)==
(TensorProduct[v1,v2] m1)==
(KroneckerProduct[v1,v2] m1)


| improve this answer | |

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