Search code examples
matlabmatrixsparse-matrix

Create a matrix from some given matrices


I have this matrix to implement. Where user would define the value of dimensions ( p and N).

matrix

what would be the most efficient way to doing this in matlab.


Solution

  • assuming you have a function to define A(p), B(p), and C(p) you can use kron as follows:

    F = @(N,p) kron(diag(ones(N,1)),A(p)) + ... 
               kron(diag(ones(N-1,1),1),B(p)) + ...
               kron(diag(ones(N-1,1),-1),C(p))
    

    For example, if

    A = @(p) ones(p);
    B = @(p) 2*ones(p);
    C = @(p) 3*ones(p);
    
    
    
    >> F(4,2)
    
    ans =
    
     1     1     2     2     0     0     0     0
     1     1     2     2     0     0     0     0
     3     3     1     1     2     2     0     0
     3     3     1     1     2     2     0     0
     0     0     3     3     1     1     2     2
     0     0     3     3     1     1     2     2
     0     0     0     0     3     3     1     1
     0     0     0     0     3     3     1     1