# Why doesn't this Kronecker Product work with columns, but with rows?

Using the formula given in this math.stackexchange answer by the user greg

\eqalign{ vec(M\otimes dK) &= \left(\pmatrix{I_T\otimes (M \cdot e_1)\cr I_T\otimes (M \cdot e_2)\cr \vdots \cr I_T \otimes (M \cdot e_T)}\otimes I_T\right) \cdot vec(dK)\cr }

how come when I run the following code, I get False?

T = 5;
dim = 3;

dKmat = Array[dKm, {T, T}];
M1mat = Array[M1m, {dim, dim}];
Kron3 = KroneckerProduct[M1mat, dKmat];

ArrayReshape[Kron3, {Times @@ Dimensions[Kron3], 1}] ===
KroneckerProduct[
Flatten[Table[
KroneckerProduct[IdentityMatrix[T, SparseArray],
Transpose[{M1mat[[;; , i]]}]], {i, dim}], 1],
IdentityMatrix[T, SparseArray]].ArrayReshape[
dKmat, {Times @@ Dimensions[dKmat], 1}]


However, if instead of the columns of M1mat in M1mat[[;; , i]], I use the rows of M1mat, as M1mat[[i]], then I get True...

Using the rows instead of the columns doesn't make mathematical sense to me. How is that correct?

Because you use the "wrong" $$vec$$: Usually columns of a matrix are stacked by $$vec$$, not rows. You effectively use

vec[x_?MatrixQ] := Flatten[x];


but you should use

vec[x_?MatrixQ] := Flatten[Transpose[x]];


Now,

id = IdentityMatrix[T, SparseArray];
LHS = vec[KroneckerProduct[M1mat, dKmat]];
RHS = Dot[
KroneckerProduct[
Flatten[
Table[
KroneckerProduct[
id,
M1mat[[;; , {i}]]
],
{i, dim}],
1
],
id
],
vec[dKmat]
];
LHS == RHS


True

• yes, my mistake. Many thanks Henrik. ;) Commented Jan 21, 2019 at 14:12
• No problem, You're always welcome. Commented Jan 21, 2019 at 14:13