Repeating entries of blkdiag

9 visualizzazioni (ultimi 30 giorni)
SA
SA il 9 Nov 2020
Commentato: SA il 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

Risposta accettata

John D'Errico
John D'Errico il 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 Commento
SA
SA il 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;

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by