Growing Diagonal Matrix Problem

I am trying to create a diagonal matrix that can grow a # of rows and # of collumns. These collumns can be from a range of 10 to 100000 to infinite but must maintain a pattern.
For simplicity I am using a 9x9 Matrix. It seems like it is repeating 3 matrics over and over.
1 - Diagonal
1 - Ones Diagonal
1 - Zeros
A B 0 1 0 0 0 0 0
B A B 0 1 0 0 0 0
0 B A 0 0 1 0 0 0
1 0 0 A B 0 1 0 0
0 1 0 B A B 0 1 0
0 0 1 0 B A 0 0 1
0 0 0 1 0 0 A B 0
0 0 0 0 1 0 B A B
0 0 0 0 0 1 0 B A
I am having a hard time trying to seeing how to write this.

回答 (1 件)

Matt J
Matt J 2021 年 3 月 23 日
編集済み: Matt J 2021 年 3 月 24 日

0 投票

A=3; B=4;
M=[A B 0; B A B; 0 B A];
n=3;
e = ones(n,1);
Q = spdiags([e 0*e e],-1:1,n,n);
%Q=toeplitz(sparse([0,1,zeros(1,n-2)]));
result =kron(speye(n),M)+kron(Q,eye(3));
full(result)
ans = 9×9
3 4 0 1 0 0 0 0 0 4 3 4 0 1 0 0 0 0 0 4 3 0 0 1 0 0 0 1 0 0 3 4 0 1 0 0 0 1 0 4 3 4 0 1 0 0 0 1 0 4 3 0 0 1 0 0 0 1 0 0 3 4 0 0 0 0 0 1 0 4 3 4 0 0 0 0 0 1 0 4 3

4 件のコメント

WARRIOR24
WARRIOR24 2021 年 3 月 23 日
I need the big matix to be changable, like if the 9x9 can be change to a 100x100. The 9x9 is just an example.
Matt J
Matt J 2021 年 3 月 23 日
I know it's just an example. So is the value of n=3 in my code.
WARRIOR24
WARRIOR24 2021 年 3 月 23 日
Here is my next problem, what is it need to be changed to 8x8? But keep everything the same except the last row and collumn?
Matt J
Matt J 2021 年 3 月 23 日
If you mean you want to remove the last row and column, then,
result(:,end)=[]; %remove last column
result(end,:)=[]; %remove last row

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

カテゴリ

ヘルプ センター および File ExchangeOperating on Diagonal Matrices についてさらに検索

製品

質問済み:

2021 年 3 月 23 日

編集済み:

2021 年 3 月 24 日

Community Treasure Hunt

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

Start Hunting!

Translated by