# Creating an appropriate Sparse Matrix

i have a 6$$\times$$6 matrix of the form

mat = SparseArray[{Band[{2, 1}, {3, 2}] -> 1.,
Band[{1, 1}] -> {a, b, a}, Band[{1, 2}, {2, 3}] -> 1.,
Band[{4, 4}] -> {-a, -b, -a}, Band[{5, 4}, {6, 5}] -> -1,
Band[{4, 5}, {5, 6}] -> -1, Band[{2, 5}, {2, 5}] -> c,
Band[{5, 2}, {5, 2}] -> -c}, 6] // MatrixForm


I want to create a 18 $$\times$$ 18 analog of the above. The nonzero elements should appear at the appropriate places as they appear in this one. e.g. m[2,5] will go to m[3,8] for a 10$$\times$$10 version. How to do it so that I don't have to manually place elements at the appropriate place? Infact, there are four 3$$\times$$3 matrices put side by side in my above example.

It is quite hard to abstract the desired pattern from the simple, low-dimensional example.

Anyways, your goals can be achieved by expressing the positions in terms of matrix size; in your case, you seem to to want matrices of size $$2 \, (2 k + 1)$$, $$k\in \mathbb{Z}$$.

What I believe to have understood is that you want a c in the center of both the lower-left and upper-right blocks. The rest is subject to quite some free interpretation. Anyways, this should provide you with enough ideas to set up your matrices as desired.

k = 3;
n = 2 k + 1;
mat = SparseArray[
{
Band[{1, 1}] -> Join[{a}, ConstantArray[b, n - 2], {a}],
Band[{2, 1}] -> ConstantArray[1, 2 k],
Band[{1, 2}] -> ConstantArray[1, 2 k],
Band[{n + 1, n + 1}] -> Join[{a}, ConstantArray[b, n - 2], {a}],
Band[{n + 1, n + 2}] -> ConstantArray[-1, 2 k],
Band[{n + 2, n + 1}] -> ConstantArray[-1, 2 k],
{n + k + 1, k + 1} -> c,
{k + 1, n + k + 1} -> c
},
{2 n , 2 n}
];
mat // MatrixForm


$$\left( \begin{array}{cccccccccccccc} a & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 1 & b & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 1 & b & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 1 & b & 1 & 0 & 0 & 0 & 0 & 0 & c & 0 & 0 & 0 \\ 0 & 0 & 0 & 1 & b & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 1 & b & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 1 & a & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & a & -1 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & -1 & b & -1 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & -1 & b & -1 & 0 & 0 & 0 \\ 0 & 0 & 0 & c & 0 & 0 & 0 & 0 & 0 & -1 & b & -1 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & -1 & b & -1 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & -1 & b & -1 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & -1 & a \\ \end{array} \right)$$

• Many thanks. This is exactly what was needed :-)
– AtoZ
Commented Oct 25, 2018 at 2:51
• Just one more thing: Will With[{a=2,b=5,k=3, n=2k+1}, mat = SparseArray[....] work? It seems to return errors when I want to assign values to a, and b with this method. Any workaround this problem?
– AtoZ
Commented Oct 25, 2018 at 4:08
• Try With[{k = 3}, With[{a = 2, b = 5, n = 2 k + 1}, mat = SparseArray[ ...]]] instead. Commented Oct 25, 2018 at 5:34
• Perfect, Thanks :-)
– AtoZ
Commented Oct 25, 2018 at 7:40
• You're welcome. Commented Oct 25, 2018 at 7:41