2
$\begingroup$

When I calculate the outer product of two matrices I get a correct result but the output is a matrix which has matrices as entries which is really annoying to deal with when I want to use it for further calculations later. For example:

a={{0,1},{1,0}};
Outer[Times,a,IdentityMatrix[2]]

while I would like to get directly the following output:

{{0,0,1,0},{0,0,0,1},{1,0,0,0},{0,1,0,0}}

Thank you

$\endgroup$
2
  • 3
    $\begingroup$ KroneckerProduct[a, IdentityMatrix[2]]? $\endgroup$
    – kglr
    Dec 11, 2017 at 10:37
  • $\begingroup$ I guess you are looking for ArrayFlatten, see e.g. Proof of the Dirac-γ matrices identity $\endgroup$
    – Artes
    Dec 11, 2017 at 10:38

1 Answer 1

2
$\begingroup$
KroneckerProduct[a, IdentityMatrix[2]]

{{0, 0, 1, 0}, {0, 0, 0, 1}, {1, 0, 0, 0}, {0, 1, 0, 0}}

Alternatively, as suggested by Artes in comments,

ArrayFlatten[Outer[Times, a, IdentityMatrix[2]]] == %

True

$\endgroup$
2
  • $\begingroup$ Then TensorProduct is also helpful? $\endgroup$ Dec 12, 2017 at 1:58
  • $\begingroup$ @AlexanderZeng, yes, great point. $\endgroup$
    – kglr
    Dec 12, 2017 at 3:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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