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 CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by