How to build a matrix from blocks [duplicate]

This question already has an answer here:

How to enter matrices in block matrix format?

2 answers

Brief MWE

Given

Block[{mat, A, B, C, D, E, F, goal}, A = {{1, 2}, {7, 8}}; B = {{3, 4}, {9, 10}}; C = {{5, 6}, {11, 12}}; D = {{11, 22}, {77, 88}}; E = {{33, 44}, {99, 100}}; F = {{55, 66}, {110, 120}}; mat = {{A, B, C}, {D, E, F}}; goal = {{1, 2, 3, 4, 5, 6}, {7, 8, 9, 10, 11, 12}, {11, 22, 33, 44,
55, 66}, {77, 88, 99, 100, 110, 120}}; Print@mat; Print@goal;]

How can I programatically create goal from mat?

In other words, how can I combine block matrices to create a larger matrix?

Explanation and Current Progress

In the MWE, we see that mat and goal clearly share a similar structure:

we simply need to be able to remove the “interior” parentheses. However, the fundamental list structures are different.

I have attempted to solve this problem many ways, without success, typically involving Flatten or FlattenAt. My best effort has been

Block[{mat, A, B, C, D, E, F, goal}, A = {{1, 2}, {7, 8}}; B = {{3, 4}, {9, 10}}; C = {{5, 6}, {11, 12}}; D = {{11, 22}, {77, 88}}; E = {{33, 44}, {99,100}}; F = {{55, 66}, {110, 120}}; mat = {{A, B, C}, {D, E, F}};goal = {{1, 2, 3, 4, 5, 6}, {7, 8, 9, 10, 11, 12}, {11, 22, 33, 44,
55, 66}, {77, 88, 99, 100, 110, 120}};Partition[Flatten@mat, (Dimensions[mat][[2]])*(Dimensions[mat][[4]])]]

for which the crux of it is

Partition[Flatten@mat, (Dimensions[mat][[2]])*(Dimensions[mat][[4]])]

However, this doesn’t produced the desired output, but rather, a scrambled matrix of the correct dimension:

Moreover, that “solution” seems poorly formed anyway, and not very robust to combining more general block matrices. What is a good way to accomplish this goal? In particular, I am only interested in combining a large number of (2×2) matrices into a large, square matrix, but I’m certain there should be a clean way to accomplish this.

=================

=================

1 Answer
1

=================

You can use ArrayFlatten

Block[{mat, A, B, C, D, E, F, goal}, A = {{1, 2}, {7, 8}};
B = {{3, 4}, {9, 10}}; C = {{5, 6}, {11, 12}};
D = {{11, 22}, {77, 88}}; E = {{33, 44}, {99, 100}};
F = {{55, 66}, {110, 120}}; mat = {{A, B, C}, {D, E, F}};
goal = {{1, 2, 3, 4, 5, 6}, {7, 8, 9, 10, 11, 12}, {11, 22, 33, 44,
55, 66}, {77, 88, 99, 100, 110, 120}};
Print@MatrixForm@mat;
Print@MatrixForm@goal;
Print@MatrixForm@ArrayFlatten[mat];

]

  

 

Thank you very much! Damn, I’m ashamed I didn’t find that command from the Flatten documentation “see also.” Sorry for wasting everyone’s time…
– Steve
Feb 25 at 0:58

1

 

@Steve no worries, I’ve suffered through the same pre-ArrayFlatten world as you. I think I accidentally selected it from the autocomplete to originally discover it!
– Peter Roberge
Feb 25 at 1:21