Extend a matrix diagonally

조회 수: 6 (최근 30일)
WARRIOR24
WARRIOR24 2021년 3월 24일
편집: Matt J 2021년 3월 24일
I am trying to exted a 4 x 4 matrix to... lets say a 9 x 9 matrix while keeping the diagonal numbers. BUT the 9 x 9 matrix can easily be changed to 11 x 11 and so and so on. I looking for a for loop that will be to scale this matrix depending on input numbers.
2 1 0 0
1 2 1 0
0 1 2 1
0 0 1 2
2 1 0 0 0 0 0 0 0
1 2 1 0 0 0 0 0 0
0 1 2 1 0 0 0 0 0
0 0 1 2 1 0 0 0 0
0 0 0 1 2 1 0 0 0
0 0 0 0 1 2 1 0 0
0 0 0 0 0 1 2 1 0
0 0 0 0 0 0 1 2 1
0 0 0 0 0 0 0 1 2

답변 (2개)

Matt J
Matt J 2021년 3월 24일
편집: Matt J 2021년 3월 24일
No need for a loop:
n=9;
result=toeplitz([2 1 zeros(1,n-2)])
result = 9×9
2 1 0 0 0 0 0 0 0 1 2 1 0 0 0 0 0 0 0 1 2 1 0 0 0 0 0 0 0 1 2 1 0 0 0 0 0 0 0 1 2 1 0 0 0 0 0 0 0 1 2 1 0 0 0 0 0 0 0 1 2 1 0 0 0 0 0 0 0 1 2 1 0 0 0 0 0 0 0 1 2
n=11;
result=toeplitz([2 1 zeros(1,n-2)])
result = 11×11
2 1 0 0 0 0 0 0 0 0 0 1 2 1 0 0 0 0 0 0 0 0 0 1 2 1 0 0 0 0 0 0 0 0 0 1 2 1 0 0 0 0 0 0 0 0 0 1 2 1 0 0 0 0 0 0 0 0 0 1 2 1 0 0 0 0 0 0 0 0 0 1 2 1 0 0 0 0 0 0 0 0 0 1 2 1 0 0 0 0 0 0 0 0 0 1 2 1 0 0 0 0 0 0 0 0 0 1 2 1

Matt J
Matt J 2021년 3월 24일
편집: Matt J 2021년 3월 24일

카테고리

Help CenterFile Exchange에서 Operating on Diagonal Matrices에 대해 자세히 알아보기

태그

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by