# Joining 4 matrices into one [closed]

I have four square matrices 5x5 and I need to join them into one 10x10. I tried various commands like Join, Append, Union and none work properly. Any advice?

(matrices = RandomInteger[{0, 99}, {2, 2, 5, 5}]) // MatrixForm
ArrayFlatten[matrices] // MatrixForm


KroneckerProduct seems to be deemed as something else, however I would say that all roads lead to Rome.

fiveByFive = RandomInteger[100, {4, 5, 5}];
posMat = {{{1, 0}, {0, 0}}, {{0, 1}, {0, 0}}, {{0, 0}, {1, 0}}, {{0, 0}, {0, 1}}};


Update

posMat can have an alternative definition

posMat = SparseArray[{{1, 1, 1}, {2, 1, 2}, {3, 2, 1}, {4, 2, 2}} -> 1, {4, 2, 2}]

• Here is the problem. I am using Mathematica 4 and it doesn't have the commands listed above. What should I do? – user1765636 Apr 28 '18 at 20:53
• @user1765636 For future questions, it would be a good idea to post your Mathematica version directly into the question. Version 4 is almost 18 years old and this constrains the possible solutions considerably. – Henrik Schumacher Apr 28 '18 at 22:39

If you really have to use version 4, you might try Flatten like this:

matrices = RandomReal[{-1, 1}, {4, 5, 5}];
Flatten[Partition[matrices, 2], {{1, 3}, {2, 4}}]


However, I am not sure if this use of Flatten was already established in version 4. Just give it a try.

Another alternative is

Join @@ (Join[Sequence @@ #, 2] & /@ Partition[matrices,2])


Or maybe

Transpose[
Join @@ Transpose[
Join @@@ Transpose[Partition[matrices, 2]], {1, 3, 2}]]


will do.

• This is not what I need. I have 4 matrices n1, n2, n3, n4. Please write the command for me. – user1765636 Apr 30 '18 at 19:56
• What about some example data? The more effort you put into your question the likelier you get the answer you need. – Henrik Schumacher Apr 30 '18 at 19:58
• I have 4 5x5 matrices n1, n2, n3, n4. I need to combine them into one 10x10 matrix with n1 in the top left corner, n2 in the top right, n3 in the bottom left and n4 in the bottom right, and I am using Mathematica 4. I got above example of using join, what should I put in this command instead of parameter 'matrices'? I tried {{n1, n2},{ n3, n4}}. It doesn't work. I also tried Flatten[{{n1,n2},{n3, n4}},2]. It doesn't work. If I change 2x3, then I get one-dimensional list of 100 items. What am I doing wrong? – user1765636 Apr 30 '18 at 20:49
• I edited the answer. Try matrices = {n1, n2, n3, n4}. – Henrik Schumacher Apr 30 '18 at 20:51
• Mathematica 4 does not understand two join commands, because it repeats one of them in the printout. Any other ideas? – user1765636 May 1 '18 at 20:46