2
$\begingroup$

i have a matrix

A =
{{3., -1., 0, -1., 0, -1., 0},
 {-1., 2., -1., 0, 0, 0, 0},
 {0, -1.,1., 0, 0, 0, 0},
 {-1., 0, 0, 2., -1., 0, 0},
 {0, 0, 0, -1., 1., 0, 0},
 {-1., 0, 0, 0, 0, 2., -1.},
 {0, 0, 0, 0, 0, -1., 1.}}

I want to partition in the form

{{A11,A12,A13,A14},
 {A21,A22,A23,A24},
 {A31,A32,A33,A34},
 {A41,A42,A43,A44}}

where

A11={3};
A12=A13=A14={-1,0};
A21=A31=A41={{-1},{0}};
A22=A33=A44={{2,-1},{-1,1}}

and rest of the blocks are {{0,0},{0,0}}

$\endgroup$
3
$\begingroup$

You can use the undocumented internal function: Internal`PartitionRagged:

A = 
 {{3, -1, 0, -1, 0, -1, 0},
 {-1, 2, -1, 0, 0, 0, 0},
 {0, -1, 1, 0, 0, 0, 0},
 {-1, 0, 0, 2, -1, 0, 0},
 {0, 0, 0, -1, 1, 0, 0},
 {-1, 0, 0, 0, 0, 2, -1},
 {0, 0, 0, 0, 0, -1, 1}}

output = Internal`PartitionRagged[A, {{1, 2, 2, 2}, {1, 2, 2, 2}}]
{
 {{{3}},{{-1,0}},{{-1,0}},{{-1,0}}},
 {{{-1},{0}},{{2,-1},{-1,1}},{{0,0},{0,0}},{{0,0},{0,0}}},
 {{{-1},{0}},{{0,0},{0,0}},{{2,-1},{-1,1}},{{0,0},{0,0}}},
 {{{-1},{0}},{{0,0},{0,0}},{{0,0},{0,0}},{{2,-1},{-1,1}}}
}

A corresponding Grid:

Grid[A, Dividers -> {{True, {True, False}}, {True, {True, False}}}]

enter image description here

You can recombine the partitions with ArrayFlatten if needed:

ArrayFlatten[output] == A
True
$\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.