Block diagonal matrices in MATLAB?

  • MATLAB
  • Thread starter NoOne79
  • Start date
  • #26
Hello,

I have similar question, but with additional problem. I will appreciate if somebody can help me.
I want to creat block diagonal matrix (blocks 3x3, different), but the data for it saved as a 4D vector:
data = data(1:N,1:M,1:3,1:3)
I want to create main diagonal from data(2,1:M,1:3,1:3).
I mean M1 = data(2,1,1:3,1:3), M2 = data(2,2,1:3,1:3) and so on...

Thank you in advance.
 
  • #27
I found some solution to my problem (may be it isn't the best one, but improves the run time more than in 10 compare to using "for").

1. Created 5 vectors of main diagonals using coordinates with 3 step jump in new vectors. I'll better put part of my code here (vector creation)
2. Made "sparse" on all the vectors in order to improve calculation time in the future.
3. Created diagonal matrix using function "diag".

% Create vectors for three main diagonals
V_MD(1:3:3*(M-1)) = dR_dVm(i,2:M,1,1); % create main diagonal
V_MD(2:3:3*(M-1)) = dR_dVm(i,2:M,2,2);
V_MD(3:3:3*(M-1)) = dR_dVm(i,2:M,3,3);
V_MDs = sparse(V_MD); % creat sparse vector

V_UpD1 = zeros(3*(M-1)-1,1); % create right diagonal (+1)
V_UpD1(1:3:3*(M-1)-1) = dR_dVm(i,2:M,1,2);
V_UpD1(2:3:3*(M-1)-1) = dR_dVm(i,2:M,2,3);
V_UpD1s = sparse(V_UpD1);

V_UpD2 = zeros(3*(M-1)-2,1); % create right diagonal (+2)
V_UpD2(1:3:3*(M-1)-2) = dR_dVm(i,2:M,1,3);
V_UpD2s = sparse(V_UpD2);

V_lD1 = zeros(3*(M-1)-1,1); % create left diagonal (-1)
V_lD1(1:3:3*(M-1)-1) = dR_dVm(i,2:M,2,1);
V_lD1(2:3:3*(M-1)-1) = dR_dVm(i,2:M,3,2);
V_lD1s = sparse(V_lD1);

V_lD2 = zeros(3*(M-1)-2,1); % create left diagonal (-2)
V_lD2(1:3:3*(M-1)-2) = dR_dVm(i,2:M,3,1);
V_lD2s = sparse(V_lD2);

M_lines = M_linesUpDown_andTeye + diag(V_MDs,0) + diag(V_UpD1s,1) + diag(V_UpD2s,2) + diag(V_lD1s,-1) + diag(V_lD2s,-2);
 
  • #28
2
0
Dear friends
I have a same problem. I try to create a block tridiagonal matrix in matlab and I tried everything but it is not working for me. I have matrices A, B, C of dimension 4*4. these matrices are dependent on (n) and in each n has different values. I have to develop a block tridiagonal matrix D whose leading diagonal is matrix A and whose upper diagonal is matrix B and lower diagonal is Matrix C( as mentioned because these matrices are dependent to n they will be different when the row of main matrix (D) changed). I will be grateful if anyone can help me how to write it in matlab and especially as a part of M-file.
 
  • #29
1
0
Hi,
I have a similar problem. I want to create a block diagonal matrix with diagonals B1,B2,..Bn but each Bi has different size. Could anyone suggest a method to create this matrix quickly?
Thank you in advance!
Best,
Ultione
 

Related Threads on Block diagonal matrices in MATLAB?

Replies
1
Views
3K
  • Last Post
Replies
2
Views
3K
  • Last Post
Replies
5
Views
4K
Replies
1
Views
4K
  • Last Post
Replies
2
Views
2K
Replies
3
Views
1K
Replies
1
Views
3K
  • Last Post
Replies
3
Views
3K
  • Last Post
Replies
2
Views
2K
Replies
1
Views
4K
Top