Repeating entries of blkdiag

8 Ansichten (letzte 30 Tage)
SA
SA am 9 Nov. 2020
Kommentiert: SA am 10 Nov. 2020
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

Akzeptierte Antwort

John D'Errico
John D'Errico am 9 Nov. 2020
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 Kommentar
SA
SA am 10 Nov. 2020
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;

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by