You will be given a square matrix of integers M and another positive integer n, strictly smaller than the size of M. Your task is to generate all square sub-matrices of M of size n.
For the purposes of this challenge, a square sub-matrix is a group of adjacent rows and columns contained in M.
Input / Output Formats
You are free to choose any other reasonable formats, these are just some examples.
Input
- A matrix in the native matrix type (if your language has one)
- A 2D array (an array of 1D arrays, each corresponding to one row / one column)
- A 1D array (since the matrix is always square)
- A string (you chose the spacing, but please do not abuse this in any way), etc.
Output
- A matrix of matrices.
- A 4D array, where each element (3D list) represents the sub-matrices on a row/column.
- A 3D array, where each element (2D list) represents a sub-matrix.
- A string representation of the resulting sub-matrices, etc.
Specs
- You may choose to take the size of M as input too. It is guaranteed to be at least 2.
- The orientation of the output is arbitrary: you may choose to output the sub-matrices as lists of columns or lists of rows, but your choice must be consistent.
- You can compete in any programming language and can take input and provide output through any standard method, while taking note that these loopholes are forbidden by default.
- This is code-golf, so the shortest submission (in bytes) for every language wins.
Example
Given n = 3 and M:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
The possible 3x3 submatrices are:
+-------+ +--------+ 1 2 3 4 1 2 3 4 |1 2 3| 4 1| 2 3 4 | +--------+ +--------+ |5 6 7| 8 5| 6 7 8 | |5 6 7|8 5| 6 7 8| |9 10 11|12 9|10 11 12| |9 10 11|12 9|10 11 12| +-------+ +--------+ |13 14 15|16 13|14 15 16| 13 14 15 16 13 14 15 16 +--------+ +--------+
So the result would be:
[[[1, 2, 3], [5, 6, 7], [9, 10, 11]], [[2, 3, 4], [6, 7, 8], [10, 11, 12]], [[5, 6, 7], [9, 10, 11], [13, 14, 15]], [[6, 7, 8], [10, 11, 12], [14, 15, 16]]]
As noted above, an output of:
[[[1, 5, 9], [2, 6, 10], [3, 7, 11]], [[2, 6, 10], [3, 7, 11], [4, 8, 12]], [[5, 9, 13], [6, 10, 14], [7, 11, 15]], [[6, 10, 14], [7, 11, 15], [8, 12, 16]]]
would also be acceptable, if you choose to return the sub-matrices as lists of rows instead.
Test cases
The inputs M, n:
[[1,2,3],[5,6,7],[9,10,11]], 1
[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]], 3
[[100,-3,4,6],[12,11,14,8],[0,0,9,3],[34,289,-18,3]], 2
[[100,-3,4,6],[12,11,14,8],[9,10,11,12],[13,14,15,16]], 3
And the corresponding outputs (sub-matrices given as lists of rows):
[[[1]],[[2]],[[3]],[[5]],[[6]],[[7]],[[9]],[[10]],[[11]]]
[[[1,2,3],[5,6,7],[9,10,11]],[[2,3,4],[6,7,8],[10,11,12]],[[5,6,7],[9,10,11],[13,14,15]],[[6,7,8],[10,11,12],[14,15,16]]]
[[[100,-3],[12,11]],[[-3,4],[11,14]],[[4,6],[14,8]],[[12,11],[0,0]],[[11,14],[0,9]],[[14,8],[9,3]],[[0,0],[34,289]],[[0,9],[289,-18]],[[9,3],[-18,3]]]
[[[100,-3,4],[12,11,14],[9,10,11]],[[-3,4,6],[11,14,8],[10,11,12]],[[12,11,14],[9,10,11],[13,14,15]],[[11,14,8],[10,11,12],[14,15,16]]]
Or, as lists of columns:
[[[1]],[[2]],[[3]],[[5]],[[6]],[[7]],[[9]],[[10]],[[11]]]
[[[1,5,9],[2,6,10],[3,7,11]],[[2,6,10],[3,7,11],[4,8,12]],[[5,9,13],[6,10,14],[7,11,15]],[[6,10,14],[7,11,15],[8,12,16]]]
[[[100,12],[-3,11]],[[-3,11],[4,14]],[[4,14],[6,8]],[[12,0],[11,0]],[[11,0],[14,9]],[[14,9],[8,3]],[[0,34],[0,289]],[[0,289],[9,-18]],[[9,-18],[3,3]]]
[[[100,12,9],[-3,11,10],[4,14,11]],[[-3,11,10],[4,14,11],[6,8,12]],[[12,9,13],[11,10,14],[14,11,15]],[[11,10,14],[14,11,15],[8,12,16]]]]