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

4 views (last 30 days)

Commented: Keita Abe on 14 Feb 2023

##### 0 CommentsShow -2 older commentsHide -2 older comments

Sign in to comment.

### Answers (2)

Hernia Baby on 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 CommentShow -1 older commentsHide -1 older comments
Keita Abe on 14 Feb 2023
これは素晴らしい関数ですね！

Sign in to comment.

Keita Abe on 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 CommentShow -1 older commentsHide -1 older comments
Keita Abe on 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

Sign in to comment.

### Categories

Find more on ループと条件付きステートメント in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!