# 10×10の三重対角​行列のfor文を利用​した作り方について

9 visualizzazioni (ultimi 30 giorni)

Commentato: Keita Abe il 14 Feb 2023

##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposte (2)

Hernia Baby il 10 Feb 2023
テプリッツ行列を使えば1行です。
A = toeplitz([4 -1 zeros(1,8)])
A = 10×10
4 -1 0 0 0 0 0 0 0 0 -1 4 -1 0 0 0 0 0 0 0 0 -1 4 -1 0 0 0 0 0 0 0 0 -1 4 -1 0 0 0 0 0 0 0 0 -1 4 -1 0 0 0 0 0 0 0 0 -1 4 -1 0 0 0 0 0 0 0 0 -1 4 -1 0 0 0 0 0 0 0 0 -1 4 -1 0 0 0 0 0 0 0 0 -1 4 -1 0 0 0 0 0 0 0 0 -1 4
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
Keita Abe il 14 Feb 2023
これは素晴らしい関数ですね！

Accedi per commentare.

Keita Abe il 10 Feb 2023
for文を使わない方法ではダメですか？ diag という関数を使うと簡単にできそうでした。
v = 4*ones(1,10);
v1 = -1*ones(1,9);
D = diag(v)+diag(v1,1)+diag(v1,-1)
D = 10×10
4 -1 0 0 0 0 0 0 0 0 -1 4 -1 0 0 0 0 0 0 0 0 -1 4 -1 0 0 0 0 0 0 0 0 -1 4 -1 0 0 0 0 0 0 0 0 -1 4 -1 0 0 0 0 0 0 0 0 -1 4 -1 0 0 0 0 0 0 0 0 -1 4 -1 0 0 0 0 0 0 0 0 -1 4 -1 0 0 0 0 0 0 0 0 -1 4 -1 0 0 0 0 0 0 0 0 -1 4
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
Keita Abe il 10 Feb 2023
for文が必須ということであれば例えばこうでしょうか。
n = 10;
A = zeros(n);
for i = 2:n-1
A(i,i) = 4;
A(i,i-1) = -1;
A(i,i+1) = -1;
A(i+1,i) = -1;
A(i-1,i) = -1;
end
A(1,1) = 4;
A(n,n) = 4
A = 10×10
4 -1 0 0 0 0 0 0 0 0 -1 4 -1 0 0 0 0 0 0 0 0 -1 4 -1 0 0 0 0 0 0 0 0 -1 4 -1 0 0 0 0 0 0 0 0 -1 4 -1 0 0 0 0 0 0 0 0 -1 4 -1 0 0 0 0 0 0 0 0 -1 4 -1 0 0 0 0 0 0 0 0 -1 4 -1 0 0 0 0 0 0 0 0 -1 4 -1 0 0 0 0 0 0 0 0 -1 4

Accedi per commentare.

### Categorie

Scopri di più su ループと条件付きステートメント in Help Center e File Exchange

### Community Treasure Hunt

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

Start Hunting!