Azzera filtri
Azzera filtri

Extend a matrix diagonally

4 visualizzazioni (ultimi 30 giorni)
WARRIOR24
WARRIOR24 il 24 Mar 2021
Modificato: Matt J il 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

Risposte (2)

Matt J
Matt J il 24 Mar 2021
Modificato: Matt J il 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 il 24 Mar 2021
Modificato: Matt J il 24 Mar 2021

Categorie

Scopri di più su Operating on Diagonal Matrices in Help Center e File Exchange

Tag

Prodotti


Release

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by