Tridiagonal Matrix with subdiagonal and main diagonal is also matrix

I have two matrices A and B. I want A to be main diagonal and B to be my subdiagonals. How do I create such a matrix? By the way sizes of A and B changes but they are square matrices.
Specifically, a1=4,b1=-1
A =diag(a1*ones(1,N-1)) + diag(b1*ones(1,N-2),1) + diag(b1*ones(1,N-2),-1)
B=(-1)*eye(N-1)
These are my A and B matrices. I need to define a (N-1)*(N-1) times (N-1)*(N-1) matrix . For example for N=1000 or N=5000 I should be able to change the N value.

14 件のコメント

Stephan
Stephan 2021 年 5 月 12 日
Please give a small example of what you want as result
Mücahit Özalp
Mücahit Özalp 2021 年 5 月 12 日
編集済み: Mücahit Özalp 2021 年 5 月 12 日
@Stephan A =diag(a1*ones(1,N-1)) + diag(b1*ones(1,N-2),1) + diag(b1*ones(1,N-2),-1)
B=(-1)*eye(N-1)
These are my A and B matrices. I need to define a (N-1)*(N-1) times (N-1)*(N-1) matrix . For example for N=1000 or N=5000 I should be able to change the N value.
Walter Roberson
Walter Roberson 2021 年 5 月 12 日
I do not know what it means for a square matrix to be a subdiagonal?
Mücahit Özalp
Mücahit Özalp 2021 年 5 月 12 日
Ok let me explain,
Consider this matrix. a values are on the diagonal. b and c values are on the subdiagonal. Is it ok now ? But all the other entries should be zero.
What I want is instead of b's and c's I want to place my B matrix that I defined above. Instead of a's I want to place my A matrix that I defined above.
I am also confused by your terminology. Let's take a look at a very small example:
N = 5;
a1 = 4;
b1 = -1;
A =diag(a1*ones(1,N-1)) + diag(b1*ones(1,N-2),1) + diag(b1*ones(1,N-2),-1)
A = 4×4
4 -1 0 0 -1 4 -1 0 0 -1 4 -1 0 0 -1 4
B=(-1)*eye(N-1)
B = 4×4
-1 0 0 0 0 -1 0 0 0 0 -1 0 0 0 0 -1
It seems that you are saying that A and B, as defined above, are the inputs to the new matrix you want. Can you tell us what the output should be? Don't just describe it. Please write the output, ideally in MATLAB syntax.
Mücahit Özalp
Mücahit Özalp 2021 年 5 月 12 日
If I could that, I would not I ask it here. I can just describe it. Could you just look for block tridiagonal matrices. That is what I want.
The green ones should be my B matrices. Red part should be my A matries .
That is all I can do.
the cyclist
the cyclist 2021 年 5 月 12 日
I was not asking you to make the general algorithm. I was asking you what the output would be for that one small example.
But I think what you just posted makes it clearer.
Mücahit Özalp
Mücahit Özalp 2021 年 5 月 12 日
編集済み: Mücahit Özalp 2021 年 5 月 12 日
I am just looking for the matrix. For N=5 the matrix should be 16x16. For N=6 it sould be 25x25.
In general form [(N-1)^2] x [(N-1)^2].
Jan
Jan 2021 年 5 月 12 日
You mention the general form "[(N-1)^2] x [(N-1)^2]" and post a 12x12 matrix as example, which does not match this format.
You mention: "B=(-1)*eye(N-1)" but the example contains multiple different full 3x3 matrices.
Please post small examples e.g. for N==3 including all inputs and a manually constructed output. Maybe an N=4 example is required.
Mücahit Özalp
Mücahit Özalp 2021 年 5 月 12 日
Mücahit Özalp
Mücahit Özalp 2021 年 5 月 12 日
This is what get when N=5
Mücahit Özalp
Mücahit Özalp 2021 年 5 月 12 日
Thank you for all. I appreciate your effort.
Mücahit Özalp
Mücahit Özalp 2021 年 5 月 14 日
編集済み: Mücahit Özalp 2021 年 5 月 14 日
@Matt JIt's me again now I can form the matrix for N=5000 but I need to evaluate its smallest 10 eigenvalues using E=eigs(D,10,'smallestabs'); I get out of memory error. Is there a way to solve that.
Matt J
Matt J 2021 年 5 月 14 日
I don't think so, but if you post a new question on it (ideally with a demo), others on the forum may have some thoughts.

 採用された回答

Matt J
Matt J 2021 年 5 月 12 日
編集済み: Matt J 2021 年 5 月 12 日
So, you want a block Toeplitz matrix?
N = 5;
A =diag([7 4 4]);
B=[8 8 10; 2 5 2; 10 8 7];
C=zeros(3);
blocks={C,B,A};
result=cell2mat(blocks( toeplitz(1+[2,1,zeros(1,N-2)]) ))
result = 15×15
7 0 0 8 8 10 0 0 0 0 0 0 0 0 0 0 4 0 2 5 2 0 0 0 0 0 0 0 0 0 0 0 4 10 8 7 0 0 0 0 0 0 0 0 0 8 8 10 7 0 0 8 8 10 0 0 0 0 0 0 2 5 2 0 4 0 2 5 2 0 0 0 0 0 0 10 8 7 0 0 4 10 8 7 0 0 0 0 0 0 0 0 0 8 8 10 7 0 0 8 8 10 0 0 0 0 0 0 2 5 2 0 4 0 2 5 2 0 0 0 0 0 0 10 8 7 0 0 4 10 8 7 0 0 0 0 0 0 0 0 0 8 8 10 7 0 0 8 8 10

8 件のコメント

Mücahit Özalp
Mücahit Özalp 2021 年 5 月 12 日
@Matt J Yeah. I guess. I didn't know the term Toeplitz.
Mücahit Özalp
Mücahit Özalp 2021 年 5 月 12 日
@Matt J How could apply it to my problem. For the matrices given A,B above. And get [(N-1)^2] x [(N-1)^2] matrix.
Mücahit Özalp
Mücahit Özalp 2021 年 5 月 12 日
@Matt JWhen I apply it to my problem I get 20x20 matrix in the result. If I use the code that you write
Mücahit Özalp
Mücahit Özalp 2021 年 5 月 12 日
I made the N-2 in your code N-3 and it worked. Thak you so much. @Matt J
Matt J
Matt J 2021 年 5 月 12 日
You have several examples above. Which one are we talking about?
Mücahit Özalp
Mücahit Özalp 2021 年 5 月 12 日
編集済み: Mücahit Özalp 2021 年 5 月 12 日
This one
a1=4,b1=-1
A =diag(a1*ones(1,N-1)) + diag(b1*ones(1,N-2),1) + diag(b1*ones(1,N-2),-1)
B=(-1)*eye(N-1)
The code does not work for N<5 and I tried N=1000 it say out of memory. This uses lots of memory I guess. How could we get rid of zeros? Maybe it makes it easy to exaluate. How could we apply sparse here.
N = 1000;
a1=4;b1=-1;
A =diag(a1*ones(1,N-1)) + diag(b1*ones(1,N-2),1) + diag(b1*ones(1,N-2),-1);
B=(-1)*speye(N-1) ;
C=sparse(N-1,N-1);
blocks={C,B,sparse(A)};
result=cell2mat(blocks( toeplitz(1+[2,1,zeros(1,N-3)]) ));
whos result
Name Size Bytes Class Attributes result 998001x998001 103680256 double sparse
Mücahit Özalp
Mücahit Özalp 2021 年 5 月 12 日
@Matt J This works now. Awesome.

その他の回答 (1 件)

Matt J
Matt J 2021 年 5 月 12 日
Here's another way, probably much faster.
N=1000;
a1=4;b1=-1;
A =diag(a1*ones(1,N-1)) + diag(b1*ones(1,N-2),1) + diag(b1*ones(1,N-2),-1);
B=(-1)*eye(N-1);
E0=speye(N);
E1=E0(2:end,1:end-1);
E0=E0(1:end-1,1:end-1);
result=kron(E0,A) + kron(E1,B)+kron(E1.',B);
whos result
Name Size Bytes Class Attributes result 998001x998001 87760160 double sparse

1 件のコメント

Mücahit Özalp
Mücahit Özalp 2021 年 5 月 12 日
Yeah much much faster.

This question is locked.

カテゴリ

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

タグ

質問済み:

2021 年 5 月 12 日

Locked:

2024 年 7 月 13 日

Community Treasure Hunt

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

Start Hunting!

Translated by