2
$\begingroup$

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.

$\endgroup$
4
$\begingroup$

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)$$

$\endgroup$
  • $\begingroup$ Many thanks. This is exactly what was needed :-) $\endgroup$ – AtoZ Oct 25 '18 at 2:51
  • $\begingroup$ 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? $\endgroup$ – AtoZ Oct 25 '18 at 4:08
  • 1
    $\begingroup$ Try With[{k = 3}, With[{a = 2, b = 5, n = 2 k + 1}, mat = SparseArray[ ...]]] instead. $\endgroup$ – Henrik Schumacher Oct 25 '18 at 5:34
  • $\begingroup$ Perfect, Thanks :-) $\endgroup$ – AtoZ Oct 25 '18 at 7:40
  • 1
    $\begingroup$ You're welcome. $\endgroup$ – Henrik Schumacher Oct 25 '18 at 7:41

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.