0
$\begingroup$

I'm trying to construct a matrix H[i,j] that satisfies the "If" condition, in this case, if i==f then the main diagonal has a matrix stored, and also, the other non diagonal terms that are matrices too. How to return the full H[i,j] matrix from this loop?

   n = 2;
 \[Epsilon] = \[CapitalAlpha];



 For[j = 0, j < n + 1, j++,
  For[i = 0, i < n + 1, i++,
    If[i == j,

      Subscript[H, i, j] = 
       Table[If[i == j - 1, t, 0], {i, 1, n}, {j, 1, n}] + 
        Table[If[i == j + 1, t, 0], {i, 1, n}, {j, 1, n}] +
        Table[If[i == j, \[Epsilon], 0], {i, 1, n}, {j, 1, n}]
      ,

      Subscript[H, i, j] = t*IdentityMatrix[n]             ];
    ];

  ];
$\endgroup$
1
$\begingroup$
n = 2;
ArrayFlatten[
 Table[
  If[i == j,
   Table[t Boole[Abs[i - j] == 1] + ϵ Boole[i == j], {i, 1, n}, {j, 1, n}],
   t IdentityMatrix[n]
   ],
  {i, 1, n}, {j, 1, n}]
 ]
| improve this answer | |
$\endgroup$
1
$\begingroup$

Try this:

Array[Subscript[H, ##] &, {n, n}]

BTW, I am almost certain that there are more convenient ways to define your H.

| improve this answer | |
$\endgroup$

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.