0
votes

I need to create a block tridiagonal matrix in Scilab. More explicitly I would like to set up a M^2 X M^2 matrix of the format:

  B  C   0 ... 0  0 
  D  B   C  0 ... 0 
  0  D   B  C ... 0
  0 ... ... ... ...  
  0  0  ... D  B  C 
  0  0   0 ... D  B

where B,C and D are M X M matrices.

I know how to create a block diagonal matrix with help of the "sysdiag" function but I haven't found any reference for easily creating a block tridiagonal one.

By the way given the matrices B,C and D I know how to create the matrix above.

 auxA = sysdiag(B,B);
  auxC = C;
  auxD = D;
  for i=1:2*M-1
     auxA = sysdiag(auxA,B);
     auxC = sysdiag(auxC,C);
     auxD = sysdiag(auxD,D);
  end
  A = auxA + [zeros((2*M+1)^2 -(2*M+1),2*M+1) auxC ;zeros(2*M+1,2*M+1) zeros(2*M+1,(2*M+1)^2 -(2*M+1)) ] + [zeros(2*M+1,(2*M+1)^2 -(2*M+1)) zeros(2*M+1,2*M+1); auxD zeros((2*M+1)^2 -(2*M+1),2*M+1)];

I would still like to knwo if there is a function that does it directly in Scilab, Matlab or R. Could someone help me with that please?

All advices are appreciated.

Many thanks

1
Doesn't look like there is. The only utility I know generating a tri-diagonal matrix, but not with the elements being matrices... only scalars. However, this submission to MATLAB FEX by John D'Errico is of particular interest: mathworks.com/matlabcentral/fileexchange/…rayryeng

1 Answers

0
votes

The Kronecker product, realized as .*. in Scilab, can help. Given M, B, C, D as in your post, Kronecker product with identity matrix creates the block-diagonal matrix like what you'd get with sysdiag:

eye(M,M).*.B

But the Kronecker product can also be taken with other matrices. Put 1s above the main diagonal, and take Kronecker product with C to obtain the C blocks in the desired locations:

diag(ones(M-1,1),1).*.C

Similarly for D:

diag(ones(M-1,1),-1).*.D

In one line,

A = eye(M,M).*.B + diag(ones(M-1,1),1).*.C + diag(ones(M-1,1),-1).*.D

does the job.