Вы можете сделать следующее (сохранить функцию в файл blktritoep.m
):
function C = blktritoep(A, B, nb)
% A, B must be square of and same size for this to work
% nb = number of times A gets repeated on the diagonal of C
A = sparse(A); % Does nothing if A is already sparse
B = sparse(B); % Idem
C = kron(diag(ones(nb,1)), A) + kron(diag(ones(nb-1,1), 1), B) + ...
kron(diag(ones(nb-1,1), -1), B);
end
Вывод, который я получаю, скажем,
A = ones(2,2);
B = -2 * ones(2,2);
C = blktritoep(A, B, 3);
full(C) % Result is sparse, just for pretty printing!
есть
ans =
1 1 -2 -2 0 0
1 1 -2 -2 0 0
-2 -2 1 1 -2 -2
-2 -2 1 1 -2 -2
0 0 -2 -2 1 1
0 0 -2 -2 1 1