Creating a Matrix with a pattern

1 回表示 (過去 30 日間)
James Wilson
James Wilson 2021 年 5 月 27 日
回答済み: Walter Roberson 2021 年 5 月 27 日
I am trying to create a pattern that starts to follow a pattern starting at the second row in the first column, then on the third row but starts on the second column and continues until an n term, creating a [n;n] matrix. The pattern is similar to this matrix:
A = [ 2,-2,0,0,0,0,0,0,0,0,0; -2,4,-2,0,0,0,0,0,0,0,0; 0,-2,4,-2,0,0,0,0,0,0,0; 0,0,-2,4,-2,0,0,0,0,0,0;
0,0,0,-2,4,-2,0,0,0,0,0; 0,0,0,0,-2,4,-2,0,0,0,0; 0,0,0,0,0,-2,4,-2,0,0,0; 0,0,0,0,0,0,-2,4,-2,0,0;
0,0,0,0,0,0,0,-2,4,-2,0; 0,0,0,0,0,0,0,0,-2,4,-2; 0,0,0,0,0,0,0,0,0,-2,4]
I am wondering how I can replicate the pattern above in a similar code then typing it out.
  1 件のコメント
Walter Roberson
Walter Roberson 2021 年 5 月 27 日
A = [ 2,-2,0,0,0,0,0,0,0,0,0; -2,4,-2,0,0,0,0,0,0,0,0; 0,-2,4,-2,0,0,0,0,0,0,0; 0,0,-2,4,-2,0,0,0,0,0,0;
0,0,0,-2,4,-2,0,0,0,0,0; 0,0,0,0,-2,4,-2,0,0,0,0; 0,0,0,0,0,-2,4,-2,0,0,0; 0,0,0,0,0,0,-2,4,-2,0,0;
0,0,0,0,0,0,0,-2,4,-2,0; 0,0,0,0,0,0,0,0,-2,4,-2; 0,0,0,0,0,0,0,0,0,-2,4]
A = 11×11
2 -2 0 0 0 0 0 0 0 0 0 -2 4 -2 0 0 0 0 0 0 0 0 0 -2 4 -2 0 0 0 0 0 0 0 0 0 -2 4 -2 0 0 0 0 0 0 0 0 0 -2 4 -2 0 0 0 0 0 0 0 0 0 -2 4 -2 0 0 0 0 0 0 0 0 0 -2 4 -2 0 0 0 0 0 0 0 0 0 -2 4 -2 0 0 0 0 0 0 0 0 0 -2 4 -2 0 0 0 0 0 0 0 0 0 -2 4 -2

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2021 年 5 月 27 日
N = 11;
diag([2, 4*ones(1,N-2), 2]) + diag(-2*ones(1,N-1),1) + diag(-2*ones(1,N-1),-1)
ans = 11×11
2 -2 0 0 0 0 0 0 0 0 0 -2 4 -2 0 0 0 0 0 0 0 0 0 -2 4 -2 0 0 0 0 0 0 0 0 0 -2 4 -2 0 0 0 0 0 0 0 0 0 -2 4 -2 0 0 0 0 0 0 0 0 0 -2 4 -2 0 0 0 0 0 0 0 0 0 -2 4 -2 0 0 0 0 0 0 0 0 0 -2 4 -2 0 0 0 0 0 0 0 0 0 -2 4 -2 0 0 0 0 0 0 0 0 0 -2 4 -2

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by