How to create a large matrix using another matrix

13 次查看(过去 30 天)
Hello everyone, I want to make a large matrix (10^7 x 10^7) but it needs to have the following matrix being repeated around its main diagonal.
a=4;
A=[1 0 a+1 0;
a+2 2 0 a+1;
0 a+2 3 0;
0 0 a+2 4];
As you can see it is a main diagonal with 1:10^7 and 1 row lower repeating the number 6 and 2 rows higher repeating the number 5.
Everything I have tried turns out to be huge in terms of memory and unable to be performed like that. I suppose the trick is by somehow making use of a sparse matrix, but I cannot get it to work properly.
Thanks a lot in advance!
  8 个评论
Walter Roberson
Walter Roberson 2020-6-1
Your original request shows the a+2 below the diagonal, so anything that ends up with the 6 above the diagonal is a wrong approach ;-)
The approach I used of constructing columns instead of rows has the advantage of not needing to transpose Acom, and so is more efficient.
Petros Tsitouras
Petros Tsitouras 2020-6-1
Then I will have to aggree with you and tell you a huge thanks once more!! Have a good day!

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2020-5-30
编辑:Walter Roberson 2020-6-1
  4 个评论
Petros Tsitouras
Petros Tsitouras 2020-5-30
Got it! Thanks a alot for the help! Is there anyway to upscale this to a 10^7 x 10^7 matrix?
Walter Roberson
Walter Roberson 2020-5-30
a=4;
N=1E7;
Adiag=(1:N).';
A1=ones(N,1)*(a+2);
A2=ones(N,1)*(a+1);
Acom=[A2, Adiag, zeros(N,1), A1];
B=spdiags(Acom,-1:2,N,N);

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Operating on Diagonal Matrices 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by