Creating a tridiagonal matrix in Matlab with smaller matrices

478 Views Asked by At

This is the problem. Given a matrix B =

\begin{bmatrix} 6&-1&0\\-1&6&-1\\0&-1&6 \end{bmatrix}.

and

I = 3 x 3 identity matrix, how can I construct D = \begin{bmatrix} B&-I&0\\-I&B&-I\\0&-I&B \end{bmatrix}?

I know that B can be easily created using B = full(gallery('tridiag',3,[-1 6 -1])); Is there something similar for D?

1

There are 1 best solutions below

0
On
C = [0 1 0; 0 0 1; 0 0 0];
kron(eye(3),B) + kron(C,-eye(3)) + kron(C',-eye(3))