How can I get all 4 × 4 submatrices of an n × n matrix?

I have a square matrix, I need to extract all possible combinations of 4 × 4 submatrices, where $$n > 4$$. For example in the case of a 6 × 6 matrix, there are 15 4 × 4 submatrices. I need the list of all 4 × 4 submatrices.

I tried with Subsets but I get matrices of 4 × 6.

• isn't the case that there are only 9 4x4 matrices? – Fraccalo Jan 23 '20 at 20:26
• I think so, in my problem the combination of 6 in 4 is 15 but there are degeneration then there are 9 4x4 matrices. How can i get those 9 matrices? – Elvis A. García Jan 23 '20 at 20:30
• I get 225 4x4 submatrices from a 6x6 matrix. – vi pa Jan 23 '20 at 21:03
• If you have a 6X6 matrix and you want get a 4x4 matrix you must delete 2 rows and 2 columns. The possible columns that you can delete are the following two rows: (1,2), (1,3), (1,4), (1,5), (1,6), (2,3), (2,4), (2,5), (2,6), (3,4), (3,5), (3,6), (4,5), (4,6), (5,6). Therefore there are 15 possibility for the rows and the same for the columns. Therefore the number of possible 4x4 submatrix of 6x6 matrix are 15*15=225. – vi pa Jan 23 '20 at 21:55

You can use Subsets:

mat = Array[Subscript[a, ##]&, {6, 6}];

TeXForm @ MatrixForm @ mat


$$\left( \begin{array}{cccccc} a_{1,1} & a_{1,2} & a_{1,3} & a_{1,4} & a_{1,5} & a_{1,6} \\ a_{2,1} & a_{2,2} & a_{2,3} & a_{2,4} & a_{2,5} & a_{2,6} \\ a_{3,1} & a_{3,2} & a_{3,3} & a_{3,4} & a_{3,5} & a_{3,6} \\ a_{4,1} & a_{4,2} & a_{4,3} & a_{4,4} & a_{4,5} & a_{4,6} \\ a_{5,1} & a_{5,2} & a_{5,3} & a_{5,4} & a_{5,5} & a_{5,6} \\ a_{6,1} & a_{6,2} & a_{6,3} & a_{6,4} & a_{6,5} & a_{6,6} \\ \end{array} \right)$$

fourbyfours =  mat[[#, #]] & /@ Subsets[Range[6], {4}];

TeXForm[Grid @ Partition[MatrixForm /@ fourbyfours, 3]]


$$\tiny\begin{array}{ccc} \left( \begin{array}{cccc} a_{1,1} & a_{1,2} & a_{1,3} & a_{1,4} \\ a_{2,1} & a_{2,2} & a_{2,3} & a_{2,4} \\ a_{3,1} & a_{3,2} & a_{3,3} & a_{3,4} \\ a_{4,1} & a_{4,2} & a_{4,3} & a_{4,4} \\ \end{array} \right) & \left( \begin{array}{cccc} a_{1,1} & a_{1,2} & a_{1,3} & a_{1,5} \\ a_{2,1} & a_{2,2} & a_{2,3} & a_{2,5} \\ a_{3,1} & a_{3,2} & a_{3,3} & a_{3,5} \\ a_{5,1} & a_{5,2} & a_{5,3} & a_{5,5} \\ \end{array} \right) & \left( \begin{array}{cccc} a_{1,1} & a_{1,2} & a_{1,3} & a_{1,6} \\ a_{2,1} & a_{2,2} & a_{2,3} & a_{2,6} \\ a_{3,1} & a_{3,2} & a_{3,3} & a_{3,6} \\ a_{6,1} & a_{6,2} & a_{6,3} & a_{6,6} \\ \end{array} \right) \\ \left( \begin{array}{cccc} a_{1,1} & a_{1,2} & a_{1,4} & a_{1,5} \\ a_{2,1} & a_{2,2} & a_{2,4} & a_{2,5} \\ a_{4,1} & a_{4,2} & a_{4,4} & a_{4,5} \\ a_{5,1} & a_{5,2} & a_{5,4} & a_{5,5} \\ \end{array} \right) & \left( \begin{array}{cccc} a_{1,1} & a_{1,2} & a_{1,4} & a_{1,6} \\ a_{2,1} & a_{2,2} & a_{2,4} & a_{2,6} \\ a_{4,1} & a_{4,2} & a_{4,4} & a_{4,6} \\ a_{6,1} & a_{6,2} & a_{6,4} & a_{6,6} \\ \end{array} \right) & \left( \begin{array}{cccc} a_{1,1} & a_{1,2} & a_{1,5} & a_{1,6} \\ a_{2,1} & a_{2,2} & a_{2,5} & a_{2,6} \\ a_{5,1} & a_{5,2} & a_{5,5} & a_{5,6} \\ a_{6,1} & a_{6,2} & a_{6,5} & a_{6,6} \\ \end{array} \right) \\ \left( \begin{array}{cccc} a_{1,1} & a_{1,3} & a_{1,4} & a_{1,5} \\ a_{3,1} & a_{3,3} & a_{3,4} & a_{3,5} \\ a_{4,1} & a_{4,3} & a_{4,4} & a_{4,5} \\ a_{5,1} & a_{5,3} & a_{5,4} & a_{5,5} \\ \end{array} \right) & \left( \begin{array}{cccc} a_{1,1} & a_{1,3} & a_{1,4} & a_{1,6} \\ a_{3,1} & a_{3,3} & a_{3,4} & a_{3,6} \\ a_{4,1} & a_{4,3} & a_{4,4} & a_{4,6} \\ a_{6,1} & a_{6,3} & a_{6,4} & a_{6,6} \\ \end{array} \right) & \left( \begin{array}{cccc} a_{1,1} & a_{1,3} & a_{1,5} & a_{1,6} \\ a_{3,1} & a_{3,3} & a_{3,5} & a_{3,6} \\ a_{5,1} & a_{5,3} & a_{5,5} & a_{5,6} \\ a_{6,1} & a_{6,3} & a_{6,5} & a_{6,6} \\ \end{array} \right) \\ \left( \begin{array}{cccc} a_{1,1} & a_{1,4} & a_{1,5} & a_{1,6} \\ a_{4,1} & a_{4,4} & a_{4,5} & a_{4,6} \\ a_{5,1} & a_{5,4} & a_{5,5} & a_{5,6} \\ a_{6,1} & a_{6,4} & a_{6,5} & a_{6,6} \\ \end{array} \right) & \left( \begin{array}{cccc} a_{2,2} & a_{2,3} & a_{2,4} & a_{2,5} \\ a_{3,2} & a_{3,3} & a_{3,4} & a_{3,5} \\ a_{4,2} & a_{4,3} & a_{4,4} & a_{4,5} \\ a_{5,2} & a_{5,3} & a_{5,4} & a_{5,5} \\ \end{array} \right) & \left( \begin{array}{cccc} a_{2,2} & a_{2,3} & a_{2,4} & a_{2,6} \\ a_{3,2} & a_{3,3} & a_{3,4} & a_{3,6} \\ a_{4,2} & a_{4,3} & a_{4,4} & a_{4,6} \\ a_{6,2} & a_{6,3} & a_{6,4} & a_{6,6} \\ \end{array} \right) \\ \left( \begin{array}{cccc} a_{2,2} & a_{2,3} & a_{2,5} & a_{2,6} \\ a_{3,2} & a_{3,3} & a_{3,5} & a_{3,6} \\ a_{5,2} & a_{5,3} & a_{5,5} & a_{5,6} \\ a_{6,2} & a_{6,3} & a_{6,5} & a_{6,6} \\ \end{array} \right) & \left( \begin{array}{cccc} a_{2,2} & a_{2,4} & a_{2,5} & a_{2,6} \\ a_{4,2} & a_{4,4} & a_{4,5} & a_{4,6} \\ a_{5,2} & a_{5,4} & a_{5,5} & a_{5,6} \\ a_{6,2} & a_{6,4} & a_{6,5} & a_{6,6} \\ \end{array} \right) & \left( \begin{array}{cccc} a_{3,3} & a_{3,4} & a_{3,5} & a_{3,6} \\ a_{4,3} & a_{4,4} & a_{4,5} & a_{4,6} \\ a_{5,3} & a_{5,4} & a_{5,5} & a_{5,6} \\ a_{6,3} & a_{6,4} & a_{6,5} & a_{6,6} \\ \end{array} \right) \\ \end{array}$$

Alternatively, you can use Minors:

pminors = Diagonal @ Minors[mat, 4, Identity];
pminors == fourbyfours


True

If you want submatrices with consecutive indices:

fourbyfoursconsec = Join @@ Partition[mat, {4, 4}, {1, 1}];
Length@fourbyfoursconsec


9

TeXForm[Grid[Partition[MatrixForm /@ fourbyfoursconsec, 3]]]


$$\tiny\begin{array}{ccc} \left( \begin{array}{cccc} a_{1,1} & a_{1,2} & a_{1,3} & a_{1,4} \\ a_{2,1} & a_{2,2} & a_{2,3} & a_{2,4} \\ a_{3,1} & a_{3,2} & a_{3,3} & a_{3,4} \\ a_{4,1} & a_{4,2} & a_{4,3} & a_{4,4} \\ \end{array} \right) & \left( \begin{array}{cccc} a_{1,2} & a_{1,3} & a_{1,4} & a_{1,5} \\ a_{2,2} & a_{2,3} & a_{2,4} & a_{2,5} \\ a_{3,2} & a_{3,3} & a_{3,4} & a_{3,5} \\ a_{4,2} & a_{4,3} & a_{4,4} & a_{4,5} \\ \end{array} \right) & \left( \begin{array}{cccc} a_{1,3} & a_{1,4} & a_{1,5} & a_{1,6} \\ a_{2,3} & a_{2,4} & a_{2,5} & a_{2,6} \\ a_{3,3} & a_{3,4} & a_{3,5} & a_{3,6} \\ a_{4,3} & a_{4,4} & a_{4,5} & a_{4,6} \\ \end{array} \right) \\ \left( \begin{array}{cccc} a_{2,1} & a_{2,2} & a_{2,3} & a_{2,4} \\ a_{3,1} & a_{3,2} & a_{3,3} & a_{3,4} \\ a_{4,1} & a_{4,2} & a_{4,3} & a_{4,4} \\ a_{5,1} & a_{5,2} & a_{5,3} & a_{5,4} \\ \end{array} \right) & \left( \begin{array}{cccc} a_{2,2} & a_{2,3} & a_{2,4} & a_{2,5} \\ a_{3,2} & a_{3,3} & a_{3,4} & a_{3,5} \\ a_{4,2} & a_{4,3} & a_{4,4} & a_{4,5} \\ a_{5,2} & a_{5,3} & a_{5,4} & a_{5,5} \\ \end{array} \right) & \left( \begin{array}{cccc} a_{2,3} & a_{2,4} & a_{2,5} & a_{2,6} \\ a_{3,3} & a_{3,4} & a_{3,5} & a_{3,6} \\ a_{4,3} & a_{4,4} & a_{4,5} & a_{4,6} \\ a_{5,3} & a_{5,4} & a_{5,5} & a_{5,6} \\ \end{array} \right) \\ \left( \begin{array}{cccc} a_{3,1} & a_{3,2} & a_{3,3} & a_{3,4} \\ a_{4,1} & a_{4,2} & a_{4,3} & a_{4,4} \\ a_{5,1} & a_{5,2} & a_{5,3} & a_{5,4} \\ a_{6,1} & a_{6,2} & a_{6,3} & a_{6,4} \\ \end{array} \right) & \left( \begin{array}{cccc} a_{3,2} & a_{3,3} & a_{3,4} & a_{3,5} \\ a_{4,2} & a_{4,3} & a_{4,4} & a_{4,5} \\ a_{5,2} & a_{5,3} & a_{5,4} & a_{5,5} \\ a_{6,2} & a_{6,3} & a_{6,4} & a_{6,5} \\ \end{array} \right) & \left( \begin{array}{cccc} a_{3,3} & a_{3,4} & a_{3,5} & a_{3,6} \\ a_{4,3} & a_{4,4} & a_{4,5} & a_{4,6} \\ a_{5,3} & a_{5,4} & a_{5,5} & a_{5,6} \\ a_{6,3} & a_{6,4} & a_{6,5} & a_{6,6} \\ \end{array} \right) \\ \end{array}$$

• I tried with Subsets, but i get 4x6 matrices, I need 4x4 matrices – Elvis A. García Jan 23 '20 at 20:20
• @Elvis, pls see the updated version. – kglr Jan 23 '20 at 20:44
• Minors[] is definitely the correct function to use here. – J. M.'s torpor Jan 24 '20 at 3:20
• Shouldn't the number of submatrixes of a 6 * 6 matrix be Binomial[6, 4] Binomial[6, 4]. – A little mouse on the pampas Jan 24 '20 at 3:37
• @Gowiththewind, right. There are 225 4X4 submatrices and Minors[mat, 4, Identity] gives all of them. (15 of these have identical row and column indices; and 9 of the latter have consecutive indices.) – kglr Jan 24 '20 at 3:56
mat = Array[a, {6, 6}];

Join @@ Table[mat[[j ;; j + 3, k ;; k + 3]], {j, Length@mat - 3}, {k, Length@mat - 3}]


$$\left\{\left( \begin{array}{cccc} a_{1,1} & a_{1,2} & a_{1,3} & a_{1,4} \\ a_{2,1} & a_{2,2} & a_{2,3} & a_{2,4} \\ a_{3,1} & a_{3,2} & a_{3,3} & a_{3,4} \\ a_{4,1} & a_{4,2} & a_{4,3} & a_{4,4} \\ \end{array} \right),\left( \begin{array}{cccc} a_{1,2} & a_{1,3} & a_{1,4} & a_{1,5} \\ a_{2,2} & a_{2,3} & a_{2,4} & a_{2,5} \\ a_{3,2} & a_{3,3} & a_{3,4} & a_{3,5} \\ a_{4,2} & a_{4,3} & a_{4,4} & a_{4,5} \\ \end{array} \right),\left( \begin{array}{cccc} a_{1,3} & a_{1,4} & a_{1,5} & a_{1,6} \\ a_{2,3} & a_{2,4} & a_{2,5} & a_{2,6} \\ a_{3,3} & a_{3,4} & a_{3,5} & a_{3,6} \\ a_{4,3} & a_{4,4} & a_{4,5} & a_{4,6} \\ \end{array} \right),\left( \begin{array}{cccc} a_{2,1} & a_{2,2} & a_{2,3} & a_{2,4} \\ a_{3,1} & a_{3,2} & a_{3,3} & a_{3,4} \\ a_{4,1} & a_{4,2} & a_{4,3} & a_{4,4} \\ a_{5,1} & a_{5,2} & a_{5,3} & a_{5,4} \\ \end{array} \right),\left( \begin{array}{cccc} a_{2,2} & a_{2,3} & a_{2,4} & a_{2,5} \\ a_{3,2} & a_{3,3} & a_{3,4} & a_{3,5} \\ a_{4,2} & a_{4,3} & a_{4,4} & a_{4,5} \\ a_{5,2} & a_{5,3} & a_{5,4} & a_{5,5} \\ \end{array} \right),\left( \begin{array}{cccc} a_{2,3} & a_{2,4} & a_{2,5} & a_{2,6} \\ a_{3,3} & a_{3,4} & a_{3,5} & a_{3,6} \\ a_{4,3} & a_{4,4} & a_{4,5} & a_{4,6} \\ a_{5,3} & a_{5,4} & a_{5,5} & a_{5,6} \\ \end{array} \right),\left( \begin{array}{cccc} a_{3,1} & a_{3,2} & a_{3,3} & a_{3,4} \\ a_{4,1} & a_{4,2} & a_{4,3} & a_{4,4} \\ a_{5,1} & a_{5,2} & a_{5,3} & a_{5,4} \\ a_{6,1} & a_{6,2} & a_{6,3} & a_{6,4} \\ \end{array} \right),\left( \begin{array}{cccc} a_{3,2} & a_{3,3} & a_{3,4} & a_{3,5} \\ a_{4,2} & a_{4,3} & a_{4,4} & a_{4,5} \\ a_{5,2} & a_{5,3} & a_{5,4} & a_{5,5} \\ a_{6,2} & a_{6,3} & a_{6,4} & a_{6,5} \\ \end{array} \right),\left( \begin{array}{cccc} a_{3,3} & a_{3,4} & a_{3,5} & a_{3,6} \\ a_{4,3} & a_{4,4} & a_{4,5} & a_{4,6} \\ a_{5,3} & a_{5,4} & a_{5,5} & a_{5,6} \\ a_{6,3} & a_{6,4} & a_{6,5} & a_{6,6} \\ \end{array} \right)\right\}$$

Might not be so elegant, but try this:

(* create a test array *)
startingArray = ArrayReshape[Range[36], {6, 6}];
startingArray // MatrixForm
all4x4 = Partition[Partition[#, 4, 1] & /@ startingArray, 4, 1];
MatrixForm[#] & /@ Flatten[Transpose[#] & /@ all4x4, 1]


This yields the expected 9 arrays.