12
$\begingroup$

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

enter image description here

enter image description here

MY TRIAL

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

enter image description here

Row[
 {Partition[lst, {4, 4}, {3, 3}] // MatrixForm,
  Partition[lst, {5, 5}, {4, 4}] // MatrixForm}]

enter image description here

ANOTHER TRIAL

Partition[lst, {3, 3}, {2, 2}, 1, {}] // MatrixForm

enter image description here

Partition[lst, {4, 4}, {3, 3}, 1, {}] // MatrixForm

enter image description here

Partition[lst, {3, 4}, {2, 3}, 1, {}] // MatrixForm

enter image description here

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:)

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

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[[1]],spec[[2]], spec[[3]], {}]
                           )
        ],
        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"] &

list of partition results

$\endgroup$
  • $\begingroup$ Very appreciated! but I need some time to understand your solution:) $\endgroup$ – xyz Oct 31 '15 at 2:51
  • $\begingroup$ Your post is a new idea for me, I can learn something that I don;t know by that:) $\endgroup$ – xyz Nov 1 '15 at 13:58
4
$\begingroup$
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}]

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.