Re: Constructing matrix out of submatrices
- To: mathgroup at smc.vnet.net
- Subject: [mg113789] Re: Constructing matrix out of submatrices
- From: Bill Rowe <readnews at sbcglobal.net>
- Date: Sat, 13 Nov 2010 00:57:56 -0500 (EST)
On 11/12/10 at 5:26 AM, sschmitt at physi.uni-heidelberg.de (Sebastian Schmitt) wrote: >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}} Here is a simple way to achieve your goal: In[2]:= SparseArray[Band[{1, 1}] -> submatrix, {3, 3}] // Normal Out[2]= {{a, b, 0}, {c, d, 0}, {0, 0, 0}} In[3]:= SparseArray[Band[{1, 2}] -> submatrix, {3, 3}] // Normal Out[3]= {{0, a, b}, {0, c, d}, {0, 0, 0}} Here, I used Normal simply to show the goal was achieved. And note, I am *constructing* the matrix from the submatrix rather than *replacing* a portion of a larger matrix with the submatrix. If I wanted the submatrix in the lower right corner of a 3 x 3 matrix, I could do it as follows: In[4]:= PadLeft[#, 3] & /@ Join[{{0, 0, 0}}, submatrix] Out[4]= {{0, 0, 0}, {0, a, b}, {0, c, d}} or In[5]:= ArrayFlatten[{{List /@ {0, 0, 0}, Join[{{0, 0}}, submatrix]}}] Out[5]= {{0, 0, 0}, {0, a, b}, {0, c, d}}