Effective matrix defining (diagonals)

1 回表示 (過去 30 日間)
Nitzan Haviv
Nitzan Haviv 2021 年 8 月 4 日
回答済み: Nitzan Haviv 2021 年 8 月 4 日
hi, i would like to get some help with defining the next matrix in the easiest way. i know the diag() function but i didnt find a way the define the non-main diagonals.

採用された回答

Stephen23
Stephen23 2021 年 8 月 4 日
N = 7;
M = toeplitz([-2,1,zeros(1,N-2)])
M = 7×7
-2 1 0 0 0 0 0 1 -2 1 0 0 0 0 0 1 -2 1 0 0 0 0 0 1 -2 1 0 0 0 0 0 1 -2 1 0 0 0 0 0 1 -2 1 0 0 0 0 0 1 -2
  2 件のコメント
Nitzan Haviv
Nitzan Haviv 2021 年 8 月 4 日
thanks a lot!
Stephen23
Stephen23 2021 年 8 月 4 日
@Nitzan Haviv: my pleasure! Remember to click the accept button if my answer helped you!

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

その他の回答 (1 件)

Nitzan Haviv
Nitzan Haviv 2021 年 8 月 4 日
btw i found another way
when N is the dimention of the square matrix
d_sec = ones(1,N-1);
d_main= ones(1,N).*-2;
H = diag(d_main)+diag(d_sec,-1) + diag(d_sec,1);

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by