Entering block matrices, general case doesn't work - Mathematica Stack Exchange most recent 30 from mathematica.stackexchange.com 2019-09-22T04:43:45Z https://mathematica.stackexchange.com/feeds/question/65999 https://creativecommons.org/licenses/by-sa/4.0/rdf https://mathematica.stackexchange.com/q/65999 2 Entering block matrices, general case doesn't work Santi https://mathematica.stackexchange.com/users/6138 2014-11-18T16:56:10Z 2014-11-21T02:42:48Z <p>I want to enter several matrices at different positions in blocks. There is already a very similar answer here: <a href="https://mathematica.stackexchange.com/a/3562/6138">https://mathematica.stackexchange.com/a/3562/6138</a></p> <p>I thought that simply generalizing this function, would yield the correct result. But something seems not to work.</p> <p>This is my function:</p> <pre><code>fmetmat[A_?MatrixQ, C_?MatrixQ, D_?MatrixQ, pos_] := SparseArray`SparseBlockMatrix[{{1, 1} -&gt; A, {1 + pos, 1} -&gt; Transpose[C], {1, 1 + pos} -&gt; C, {1 + pos, 1 + pos} -&gt; D}, Dimensions[A] + pos*Dimensions[D]]; </code></pre> <p>But what I get for any integer value of <code>pos</code> is always:</p> <p><img src="https://i.stack.imgur.com/wfcRR.png" alt="enter image description here"></p> <p>I would like to be able to fill the Matrix in this way, so that if I leave holes in between they show up in the end.</p> <p>I would like to have, for:</p> <p><img src="https://i.stack.imgur.com/LWafw.png" alt="enter image description here"></p> <p><img src="https://i.stack.imgur.com/EPaOx.png" alt="Mathematica graphics"></p> <pre><code>Dmat = IdentityMatrix </code></pre> <p>And <code>pos=7</code>, the following output</p> <p><img src="https://i.stack.imgur.com/DD62C.png" alt="Mathematica graphics"></p> <p>Does somebody understand the logic here and can help me with this?</p> <p>Thanks!</p> https://mathematica.stackexchange.com/questions/65999/-/66201#66201 3 Answer by ubpdqn for Entering block matrices, general case doesn't work ubpdqn https://mathematica.stackexchange.com/users/1997 2014-11-21T02:42:48Z 2014-11-21T02:42:48Z <p>Perhaps this is a way:</p> <pre><code>am = IdentityMatrix; cm = {{c, c, c, c, c}, {d, d, d, d, d}, {e, e, e, e, e}}; dm = IdentityMatrix ArrayFlatten[ Normal[SparseArray[{{1, 1} -&gt; w[am], {1, 6} -&gt; w[cm], {6, 1} -&gt; w[Transpose@cm], {6, 6} -&gt; w[dm]}, {6, 6}]] /. w -&gt; Sequence] // MatrixForm </code></pre> <p>where <code>w</code> is just a wrapper to allow <code>SparseArray</code> to parse.</p> <p><img src="https://i.stack.imgur.com/Ytuhz.png" alt="enter image description here"></p>