Create a loop for this matrix

2 ビュー (過去 30 日間)
Rayden Plasma
Rayden Plasma 2021 年 6 月 4 日
コメント済み: Rayden Plasma 2021 年 6 月 4 日
1 -1 0 0 0 ...
-1 2 -1 0 0 ...
0 -1 2 -1 0 ...
0 0 -1 2 -1 ...
0 0 0 -1 2 ...
As you can see, this can go on forever, I and would like to find a way to write loop for a dynamic matrix where this just keeps going unitl a certain number 'N'
  4 件のコメント
David Fletcher
David Fletcher 2021 年 6 月 4 日
編集済み: David Fletcher 2021 年 6 月 4 日
I think they are asking what the end of the final row should be - I would guess [zeros(N-2,1) -1 1]
Rayden Plasma
Rayden Plasma 2021 年 6 月 4 日
correct

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

採用された回答

David Fletcher
David Fletcher 2021 年 6 月 4 日
編集済み: David Fletcher 2021 年 6 月 4 日
Just writing something with a loop on the fly (and I stress this is neither robust, nor particularly efficient), but it may be something you can work on in the absence of better answers
clear;
N=10;
base=[1 -1 0;-1 2 -1;0 -1 1];
spliceL=0;
spliceR=N-3;
reform(1,:)=[zeros(1,spliceL) base(1,:) zeros(1,spliceR)];
for iter=2:N-1
reform(iter,:)=[zeros(1,spliceL) base(2,:) zeros(1,spliceR)];
spliceL=spliceL+1;
spliceR=spliceR-1;
end
reform(end+1,:)=[zeros(1,spliceL) base(3,end-1:end) zeros(1,spliceR-1)];
reform
reform = 10×10
1 -1 0 0 0 0 0 0 0 0 -1 2 -1 0 0 0 0 0 0 0 0 -1 2 -1 0 0 0 0 0 0 0 0 -1 2 -1 0 0 0 0 0 0 0 0 -1 2 -1 0 0 0 0 0 0 0 0 -1 2 -1 0 0 0 0 0 0 0 0 -1 2 -1 0 0 0 0 0 0 0 0 -1 2 -1 0 0 0 0 0 0 0 0 -1 2 -1 0 0 0 0 0 0 0 0 -1 1
  1 件のコメント
Rayden Plasma
Rayden Plasma 2021 年 6 月 4 日
Thank you very much!!!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by