# How do I reshape an array of data into an array of matrices of data?

I have a question that I suspect is trivial, but I have not stumbled onto any functions that have made such a solution obvious to me. Consider an array of the following form:

$\begin{pmatrix} a_{11} & a_{12} & a_{13} & a_{14} \\ a_{21} & a_{22} & a_{23} & a_{24} \\ a_{31} & a_{32} & a_{33} & a_{34} \\ a_{41} & a_{42} & a_{43} & a_{44} \\ \end{pmatrix}$

I want a simple way to partition this into matrices with identical dimension, which in this case, could be a set of four 2x2 matrices, resulting in

$\begin{pmatrix} \begin{pmatrix} a_{11} & a_{12} \\ a_{21} & a_{22} \end{pmatrix} & \begin{pmatrix} a_{13} & a_{14} \\ a_{23} & a_{24} \\ \end{pmatrix} \\ \begin{pmatrix} a_{31} & a_{32} \\ a_{41} & a_{42} \\ \end{pmatrix} & \begin{pmatrix} a_{33} & a_{34} \\ a_{43} & a_{44} \\ \end{pmatrix} \end{pmatrix}$

Now, to generalize: I want a method to partition an $N\times N$ matrix into identical $d\times d$ submatrices, in exactly the way outlined here, and we assume of course that $d$ divides $N$. Could someone offer such a technique? Thanks in advance!

• Sorry, but unless I'm misunderstanding you this won't work in general. Consider the case Array[a, {8,8}], which I can reshape into a collection of 4x4 matrix of 2x2 matrices with ArrayReshape[Array[a, {8,8}], {2,2,2,2,2,2}]. The elements in this case will be partitioned incorrectly, as you can easily verify. Perhaps I'm not getting it? Commented Sep 28, 2016 at 6:40
• Correy has been quickier than I. Commented Sep 28, 2016 at 7:45

Partition does exactly what you want:

mat = RandomInteger[{0, 9}, {10, 10}];

Partition[mat, {5, 5}]

Partition[mat, {2, 2}]

• As suspected, with the right function the answer is trivial! Thanks very much friend :) Commented Sep 28, 2016 at 16:06

Sorry, I will not give explanation because I am in hurry

m = Table[Subscript[a, i, j], {i, 1, 4}, {j, 1, 4}]
m // MatrixForm
Flatten[m]
l = Partition[m, {2, 2}]
l // MatrixForm

In the same time I think all this is self explanatory