How to create a custom Matrix following a set of parameters

9 ビュー (過去 30 日間)
Razvan Alexandru
Razvan Alexandru 2021 年 10 月 24 日
コメント済み: Razvan Alexandru 2021 年 10 月 24 日
Hi, i'd like some help creating a matrix following these rules:
The matrix should have "n" number of rows and "n+1" number of colums.
now depending on the elements position in the matrix i'd like certain outputs.
I'll call position in the row "i" and position in column "j"
if i=j, the element should be 2.
if |i-j| = 1, the element should be -1
everything else = 0.

採用された回答

Matt J
Matt J 2021 年 10 月 24 日
n=5;
c=[2,-1,zeros(1,n-2)];
r=[c,0];
A=toeplitz(c,r)
A = 5×6
2 -1 0 0 0 0 -1 2 -1 0 0 0 0 -1 2 -1 0 0 0 0 -1 2 -1 0 0 0 0 -1 2 -1
  1 件のコメント
Razvan Alexandru
Razvan Alexandru 2021 年 10 月 24 日
thank you for the quick response!

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

その他の回答 (1 件)

Matt J
Matt J 2021 年 10 月 24 日
n=5;
A=-diff(eye(n+2),2,1);
A(:,1)=[]
A = 5×6
2 -1 0 0 0 0 -1 2 -1 0 0 0 0 -1 2 -1 0 0 0 0 -1 2 -1 0 0 0 0 -1 2 -1

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by