MathGroup Archive 2010

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Constructing matrix out of submatrices

  • To: mathgroup at smc.vnet.net
  • Subject: [mg113785] Re: Constructing matrix out of submatrices
  • From: Oliver Ruebenkoenig <ruebenko at wolfram.com>
  • Date: Sat, 13 Nov 2010 00:57:11 -0500 (EST)

On Fri, 12 Nov 2010, 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.
>
> I start with a zero matrix and a submatrix:
>
> 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
>
>


Hi Sebastian,

you could use this, which can be quite efficient.

matrix = ConstantArray[0, {3, 3}]
submatrix = {{a, b}, {c, d}}

matrix[[{1, 2}, {1, 2}]] = submatrix;
matrix

matrix = ConstantArray[0, {3, 3}];
matrix[[{1, 2}, {2, 3}]] = submatrix;

HTH,
Oliver


  • Prev by Date: sieving out selected multiples
  • Next by Date: Table local
  • Previous by thread: Constructing matrix out of submatrices
  • Next by thread: Re: Constructing matrix out of submatrices