8
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ isn't the case that there are only 9 4x4 matrices? $\endgroup$ – Fraccalo Jan 23 at 20:26
  • $\begingroup$ 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? $\endgroup$ – Elvis A. García Jan 23 at 20:30
  • $\begingroup$ I get 225 4x4 submatrices from a 6x6 matrix. $\endgroup$ – vi pa Jan 23 at 21:03
  • $\begingroup$ 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. $\endgroup$ – vi pa Jan 23 at 21:55
9
$\begingroup$

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}$

$\endgroup$
  • $\begingroup$ I tried with Subsets, but i get 4x6 matrices, I need 4x4 matrices $\endgroup$ – Elvis A. García Jan 23 at 20:20
  • $\begingroup$ @Elvis, pls see the updated version. $\endgroup$ – kglr Jan 23 at 20:44
  • 2
    $\begingroup$ Minors[] is definitely the correct function to use here. $\endgroup$ – J. M. is in limbo Jan 24 at 3:20
  • 1
    $\begingroup$ Shouldn't the number of submatrixes of a 6 * 6 matrix be Binomial[6, 4] Binomial[6, 4]. $\endgroup$ – Please Correct GrammarMistakes Jan 24 at 3:37
  • $\begingroup$ @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.) $\endgroup$ – kglr Jan 24 at 3:56
3
$\begingroup$
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\}$

$\endgroup$
1
$\begingroup$

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.
enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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