Extend a matrix diagonally

9 vues (au cours des 30 derniers jours)
WARRIOR24
WARRIOR24 le 24 Mar 2021
Modifié(e) : Matt J le 24 Mar 2021
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

Réponses (2)

Matt J
Matt J le 24 Mar 2021
Modifié(e) : Matt J le 24 Mar 2021
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 le 24 Mar 2021
Modifié(e) : Matt J le 24 Mar 2021

Catégories

En savoir plus sur Operating on Diagonal Matrices dans Help Center et File Exchange

Tags

Produits


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by