1

enter image description here

I do know how to do this for matrices on the diagonal, but I can't figure out how to do this for matrices on both the diagonal and the upper diagonal. Can someone help me?

Bert
  • 11
  • 1
  • 2
    Instead of Fold you could have used ArrayFlatten[{{a,0,0,0},{0,b,0,0},{0,0,c,0},{0,0,0,d}}]. Then replace 0's above the diagonal with matrices, if that is what you are asking. – user293787 Dec 09 '22 at 17:28
  • 2
    Can you give an example of a (small) matrix you'd like to create? – mikado Dec 09 '22 at 17:33
  • 2
    Code not images please! A minimal working Wolfram Language code example, formatted, easy to copy&paste, in Raw InputForm. – rhermans Dec 09 '22 at 17:39

1 Answers1

1

Seems like Band can be used:

foo[diag_,upperdiag_] := SparseArray[{
      Band[{1,1}]->diag,
      Band[{1,Dimensions[diag[[1]]][[2]]+1}]->upperdiag}] // Normal;

Example:

SeedRandom[1];
r[dimx_,dimy_] := RandomInteger[{1,9},{dimx,dimy}];
foo[{r[3,3],r[2,2],r[2,3],r[3,2]},{r[3,2],r[2,3],r[2,2]}] // MatrixForm

enter image description here

user293787
  • 11,833
  • 10
  • 28
  • thankyou @user293787, this is exactly what I needed! – Bert Dec 10 '22 at 13:34
  • You are welcome. Just a comment: Your question risks being closed as a duplicate of this question. If you are interested in keeping the question open, you should explain in your question (you can still edit!) why you think it is not a duplicate, or agree that it is a duplicate, whatever the case may be. Also, usually one should post code, not screenshots of code. Have a nice day! – user293787 Dec 10 '22 at 13:43