function [F,H,G] = genmat(A,B,Bd,C,n)

% Define submatrices
M = [
    A                          B
    zeros(size(B,2),size(A,1)) eye(size(B,2))
    ];
    
N = [
    B
    eye(size(B,2))
    ];

P = [
    Bd
    zeros(size(B,2),size(Bd,2))
    ];

Q = [
    C zeros(size(C,1),size(B,2))
    ];

% Generate F
F = zeros(size(Q * M,1) * n,size(Q * M,2));

j = 1;
for i = 1:size(Q * M,1):size(Q * M,1) * n
    F(i:i + size(Q * M,1) - 1,:) = Q * M^j;
    j = j + 1;
end

% Generate H
H = zeros(size(Q * M * N,1) * n);

i = 1;
for row = 1:size(Q * M * N,1):size(Q * M * N,1) * n
    j = 1;
    for col = 1:size(Q * M * N,2):size(Q * M * N,2) * n
        if (i >= j)
            H(row:row + size(Q * M * N,1) - 1,col:col + size(Q * M * N,2) - 1) = Q * M^(i - j) * N;
        end
        j = j + 1;
    end
    i = i + 1;
end

% Generate G
G = zeros(size(Q * M * P,1) * n);

i = 1;
for row = 1:size(Q * M * P,1):size(Q * M * P,1) * n
    j = 1;
    for col = 1:size(Q * M * P,2):size(Q * M * P,2) * n
        if (i >= j)
            G(row:row + size(Q * M * P,1) - 1,col:col + size(Q * M * P,2) - 1) = Q * M^(i - j) * P;
        end
        j = j + 1;
    end
    i = i + 1;
end