2

This is an example of the output that I get. I am working on with more complicated matrix, but it seemed not working. That's why I try to use a simple number to find the issue. So, my matrix is a 3x1. If I transpose it, it should be a 1x3 matrix. Then I try to multiply both matrices 3x1 * 1x3 respectively. The output should be a 3x3 matrix, right? But unfortunately, the output from Mathematica doesn't give the answer that I want. Here I attached the image of the result;enter image description here

anis
  • 23
  • 3
  • Please show us the code text rather than a screenshot of it, so we can easily test. Then, do notice that, for matrix handling, MATLAB and Mathematica are similar but not exactly the same. (You're guessing the syntax of Mathematica based on that of MATLAB, right? ) – xzczd Feb 13 '22 at 08:46
  • Also, related: https://mathematica.stackexchange.com/q/244659/1871 – xzczd Feb 13 '22 at 08:53
  • Try ky = Outer[List, {9, 7, 1}] and kt=Transpose[ky]. Use Dot (.) not *. – E. Chan-López Feb 13 '22 at 09:02
  • MMA does not have the concept of row and column vectors. Instead "Dot" acts on the last index of the left hand side and the first index of the right hand side. Read about the concept of arrays, vector , matrices in MMA. – Daniel Huber Feb 13 '22 at 09:14
  • Another related post: https://mathematica.stackexchange.com/q/574/1871 – xzczd Feb 13 '22 at 09:16
  • 2
    Compare: Dimensions[{9, 7, 1}] vs. Dimensions[{{9, 7, 1}}] vs. Dimensions[{{9}, {7}, {1}}] and then use the Dot operator for matrix multiplication where defined. – Syed Feb 13 '22 at 09:31
  • In your definition of kt you included the display wrapper MatrixForm; consequently, kt would not be a matrix. Never include display wrappers in the definition, but rather wrap the definition, i.e., MatrixForm[kt = Transpose[ky]]. – Bob Hanlon Feb 13 '22 at 15:25

2 Answers2

3

First of all, it is best to avoid assigning values wrapped in MatrixForm to variables. Values so wrapped no longer participate in algebraic expressions normally. MatrixForm should generally only be used when displaying a final result.

Second, * multiplies vectors element-wise.

Third, Mathematica tends to gloss over the distinction between column-vectors and row-vectors. For example, Transpose applied to a list (vector) returns the list unchanged:

ky === Transpose[ky]
(* True *)

Option #1 - use Dot after converting the column and row vectors to matrices

The Mathematica Dot operator produces the matrix product when applied to two matrices but only the scalar product when applied to two vectors. We can get the desired result by "upgrading" the vectors to be matrices of dimension one in the row or column direction.

krow = {ky}
(* {{9, 7, 1}} *)

kcol = Transpose[krow] (* {{9}, {7}, {1}} *)

kcol . krow // MatrixForm

Dot product

If the application is going to perform many mixed matrix and vector products, this is probably superior to the second option below.

Option #2 - use KroneckerProduct or TensorProduct on the vectors directly

We can form the matrix direct product of two lists representing vectors using KroneckerProduct or TensorProduct:

ky = {9, 7, 1};
kt = Transpose[ky]
(* {9, 7, 1}, i.e. the same as ky *)

KroneckerProduct[ky, kt] // MatrixForm

Kronecker product

TensorProduct[ky, kt] // MatrixForm

Kronecker product

TensorProduct can also be used in operator form by typing ESCt*ESC instead of the original *.

But beware that these functions will produce higher-order tensor structures than the inputs. This "upgrading" is precisely what we want in this particular case, but it may not be desirable when chaining together arbitrary matrix and vector products.

WReach
  • 68,832
  • 4
  • 164
  • 269
2

You need to make a tensor product of a vector by itself, right? If yes, you may act as follows. This is the vector:

v = {9, 7, 1};

Then this is the tensor:

t = Table[v[[i]]*v[[j]], {i, 3}, {j, 3}]

(* {{81, 63, 9}, {63, 49, 7}, {9, 7, 1}} *)

That's how its MatrixForm looks like:

t // MatrixForm

enter image description here

There is a more concise way:

t=Outer[Times, v, v]

returning the same result.

Have fun!

Alexei Boulbitch
  • 39,397
  • 2
  • 47
  • 96