2
$\begingroup$

I want to enter several matrices at different positions in blocks. There is already a very similar answer here: https://mathematica.stackexchange.com/a/3562/6138

I thought that simply generalizing this function, would yield the correct result. But something seems not to work.

This is my function:

fmetmat[A_?MatrixQ, C_?MatrixQ, D_?MatrixQ, pos_] := 
 SparseArray`SparseBlockMatrix[{{1, 1} -> A, {1 + pos, 1} -> 
 Transpose[C], {1, 1 + pos} -> C, {1 + pos, 1 + pos} -> D}, 
  Dimensions[A] + pos*Dimensions[D]];

But what I get for any integer value of pos is always:

enter image description here

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.

I would like to have, for:

enter image description here

Mathematica graphics

Dmat = IdentityMatrix[5]

And pos=7, the following output

Mathematica graphics

Does somebody understand the logic here and can help me with this?

Thanks!

$\endgroup$
  • $\begingroup$ You have described twice what you don't want, but you have not described what matrix structure you do want. $\endgroup$ – bill s Nov 19 '14 at 4:21
  • $\begingroup$ Thanks @bills I uploaded now the desired input and desired output and removed my useless function. $\endgroup$ – Santi Nov 21 '14 at 2:09
3
$\begingroup$

Perhaps this is a way:

am = IdentityMatrix[3];
cm = {{c, c, c, c, c}, {d, d, d, d, d}, {e, e, e, e, e}};
dm = IdentityMatrix[5]

ArrayFlatten[
  Normal[SparseArray[{{1, 1} -> w[am], {1, 6} -> w[cm], {6, 1} -> 
       w[Transpose@cm], {6, 6} -> w[dm]}, {6, 6}]] /. 
   w -> Sequence] // MatrixForm

where w is just a wrapper to allow SparseArray to parse.

enter image description here

$\endgroup$
  • $\begingroup$ Sorry, I forgot to accept this. It works really well, This little trick of the wrapper w is pretty nice! $\endgroup$ – Santi Nov 24 '14 at 15:07

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.