Repeating entries of blkdiag

8 vues (au cours des 30 derniers jours)
SA
SA le 9 Nov 2020
Commenté : SA le 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

Réponse acceptée

John D'Errico
John D'Errico le 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 commentaire
SA
SA le 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;

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by