Repeating entries of blkdiag

8 ビュー (過去 30 日間)
SA
SA 2020 年 11 月 9 日
コメント済み: SA 2020 年 11 月 10 日
I created a block tridiagonal matrix sysytem using the code below.
n = 4;
A = [ 6 2 0; 3 6 2; 0 3 6];
B = 7*eye(3);
D = 4*eye(3)
C = kron(eye(3),A);
C(1:end-(n-1),(n-1)+1:end) = C(1:end-(n-1),(n-1)+1:end) + blkdiag(B,B);
C((n-1)+1:end,1:end-(n-1)) = C(1:end-(n-1),(n-1)+1:end) + blkdiag(D,D);
How do i avoid typing B or D twice in blkdiag? Is there a function that can repeat B or D twice or more without having to type B or D twice or more in case my A is a huge matrix?
Thanks

採用された回答

John D'Errico
John D'Errico 2020 年 11 月 9 日
B = ones(2);
BB = repmat({B},1,5);
BDiag = blkdiag(BB{:});
spy(BDiag)
Create a cell array. Then use blkdiak properly, by turning the cell array into a comma separated list. BB{:} does that. Thus...
BB{:}
ans = 2×2
1 1 1 1
ans = 2×2
1 1 1 1
ans = 2×2
1 1 1 1
ans = 2×2
1 1 1 1
ans = 2×2
1 1 1 1
  1 件のコメント
SA
SA 2020 年 11 月 10 日
It worked thanks a lot.
This my new code.
n = 4;
A = [ 6 2 0; 3 6 2; 0 3 6];
B = 7*eye(3);
BB = repmat({B},1,2);
BDiag = blkdiag(BB{:});
D = 4*eye(3);
DD = repmat({D},1,2);
DDiag = blkdiag(DD{:});
C = kron(eye(3),A);
C(1:end-(n-1),(n-1)+1:end) = C(1:end-(n-1),(n-1)+1:end) + BDiag;
C((n-1)+1:end,1:end-(n-1)) = C((n-1)+1:end,1:end-(n-1)) + DDiag;

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by