# How to partition a 2-D array properly?

Given that I have the following array

lst = Array[Subscript[P, #1, #2] &, {5, 5}, {0, 0}]


$\left( \begin{array}{ccccc} P_{0,0} & P_{0,1} & P_{0,2} & P_{0,3} & P_{0,4} \\ P_{1,0} & P_{1,1} & P_{1,2} & P_{1,3} & P_{1,4} \\ P_{2,0} & P_{2,1} & P_{2,2} & P_{2,3} & P_{2,4} \\ P_{3,0} & P_{3,1} & P_{3,2} & P_{3,3} & P_{3,4} \\ P_{4,0} & P_{4,1} & P_{4,2} & P_{4,3} & P_{4,4} \end{array} \right)$

Then I need to partion it into the following matrix blocks  MY TRIAL

Row[
{Partition[lst, {2, 3}, {1, 2}] // MatrixForm,
Partition[lst, {3, 3}, {2, 2}] // MatrixForm,
Partition[lst, {3, 4}, {2, 3}] // MatrixForm}] Row[
{Partition[lst, {4, 4}, {3, 3}] // MatrixForm,
Partition[lst, {5, 5}, {4, 4}] // MatrixForm}] ANOTHER TRIAL

Partition[lst, {3, 3}, {2, 2}, 1, {}] // MatrixForm Partition[lst, {4, 4}, {3, 3}, 1, {}] // MatrixForm Partition[lst, {3, 4}, {2, 3}, 1, {}] // MatrixForm I searched the documentation of Partition, and I didn't discovred a directed method to deal with this problem. Could some know any simple way to do this?I really appreciate it:)

• Case 3: Partition[lst, {3, 4}, {2, 3}, {{1, 1}, {-1, 1}}, {}]. – J. M. will be back soon Aug 23 '15 at 13:31
• For the last two: remember the Nothing trick I told you about? – J. M. will be back soon Aug 23 '15 at 14:39

You can also do it layer by layer:

Clear[nestPartition]
nestPartition[array_List, nlist_List, dlist_List, klist_List] :=
Module[{matrix},
Fold[
Function[{lst, spec},
lst /.
matrix[mt_] :> (
matrix[{##}] & @@@
Partition[
mt\[Transpose],
spec[],spec[], spec[], {}]
)
],
array // matrix,
{nlist, dlist, klist}\[Transpose] // Reverse
]\[Transpose] /. matrix -> Identity
]


MapThread[
Function[{n, d, k},
nestPartition[lst, n, d, k] //
Map[Grid, #, {-4}] & // Grid[#, Dividers -> Center] & //
Column[{n, #}, Frame -> All] &
],
{
{{2, 3}, {2, 3} - 1, {{1, -1}, {1, -1}}},
{{3, 3}, {3, 3} - 1, {{1, -1}, {1, -1}}},
{{3, 4}, {3, 4} - 1, {{1, -1}, {1, 1}}},
{{4, 4}, {4, 4} - 1, {1, 1}},
{{5, 5}, {5, 5}, {1, 1}}
}\[Transpose]
] // Row[#, "\t"] & • Very appreciated! but I need some time to understand your solution:) – xyz Oct 31 '15 at 2:51
• Your post is a new idea for me, I can learn something that I don;t know by that:) – xyz Nov 1 '15 at 13:58
partitionBlock[lst_, {a_, b_}] :=
Module[{row, col, m, n},
{m, n} = Dimensions[lst, 2];
row = Mod[m - 1, a - 1];
col = Mod[n - 1, b - 1];
Which[
row == 0 && col == 0,
Partition[lst, {a, b}, {a - 1, b - 1}],
row == 0 && col != 0,
Drop[Partition[lst, {a, b}, {a - 1, b - 1}, 1, {}], -1],
row != 0 && col == 0,
Drop[Partition[lst, {a, b}, {a - 1, b - 1}, 1, {}], None, -1],
row != 0 && col != 0,
Partition[lst, {a, b}, {a - 1, b - 1}, 1, {}]
] /. {} -> Sequence[]
]


### TEST

lst1 = Array[Subscript[P, #1, #2] &, {5, 5}, {0, 0}];
Map[MatrixForm, partitionBlock[lst1, {2, 3}], {2}]

Map[MatrixForm, partitionBlock[lst1, {3, 3}], {2}]

Map[MatrixForm, partitionBlock[lst1, {3, 4}], {2}]

Map[MatrixForm, partitionBlock[lst1, {4, 4}], {2}]

Map[MatrixForm, partitionBlock[lst1, {5, 5}], {2}] 