Re: Constructing matrix out of submatrices

• To: mathgroup at smc.vnet.net
• Subject: [mg113823] Re: Constructing matrix out of submatrices
• From: Raffy <adraffy at gmail.com>
• Date: Sun, 14 Nov 2010 06:10:19 -0500 (EST)
• References: <ibl9fa\$b7l\$1@smc.vnet.net>

```On Nov 12, 9:59 pm, Patrick Scheibe <psche... at trm.uni-leipzig.de>
wrote:
> Hi,
>
>
> CreateMatrix[sub_?MatrixQ, pos : {_, _}, finaldim : {_, _}] :=
>   Transpose[{pos - {1, 1},
>     finaldim - Dimensions[sub] - pos + {1, 1}}]]
>
> CreateMatrix[{{a,b},{c,d}}, {1, 2}, {3, 3}] // MatrixForm
>
> Cheers
> Patrick
>
>
>
>
>
>
>
> On Fri, 2010-11-12 at 05:26 -0500, Sebastian Schmitt wrote:
> > Hi!
>
> > I would like to construct a matrix out of submatrices. I guess this is
> > straight forward but somehow I couldn't find the correct way in the
> > documentation. The example that follows is of course simplified. Please
> > don't take shortcuts.
>
>
> > matrix = ConstantArray[0, {3, 3}]
> > submatrix = {{a, b}, {c, d}}
>
> > I want to specificy the upper-left corner in the matrix where the
> > submatrix should be placed. If the position is {1,1} the result should =
be:
>
> > result = {{a, b, 0}, {c, d, 0}, {0, 0, 0}}
>
> > If it is {1,2}:
>
> > result = {{0, a, b}, {0, c, d}, {0, 0, 0}}
>
> > etc.
>
> > ReplacePart together with ArrayFlatten comes close, but the submatrix
> > does not "override" zeros:
>
> > ReplacePart[matrix, {1, 1} -> submatrix] // ArrayFlatten
>
> > {{a, b, 0, 0}, {c, d, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}
>
> > What are possible solutions?
>
> > Thanks,
>
> > Sebastian

A further generalization:

With[{pDim = _Integer?NonNegative},
With[{pDims = pDim | {pDim, pDim}},
ClearAll[matmush];
matmush[size : pDims, mats : {(_[_?MatrixQ, pDims]) ...} : {}] :=
Fold[#1 +
Transpose@{#2[[2]] - {1, 1},
1 + size - Dimensions[#2[[1]]] - #2[[2]]}] &,
ConstantArray[0, size*{1, 1}], mats];
];
];

matmush[{3, 5}, {
{{a, b}, {c, d}} -> 1,
{{1, 0}, {0, 1}} -> {2, 1},
Array[f, {3, 3}] -> {1, 4}
}] // MatrixForm

matmush[2] // MatrixForm

matmush[5, {Array[f,{3,3} -> 2}] // MatrixForm

```

• Prev by Date: Re: Tensor Contraction
• Next by Date: Re: Balance point of a solid
• Previous by thread: Re: Constructing matrix out of submatrices
• Next by thread: speech processing?