
#1
Jun105, 03:59 PM

P: 1

So, using MATLAB, I'm trying to create the block diagonal matrix for the central difference approximation to the 2D laplacian operator in rectangular coordinates, and I've stumbled on to what looks like a pretty useful function. It's called "blkdiag", and it works by taking all of the input arguments, which are matrices, and placing them on the diagonal of a larger sparse matrix.
The problem I have with this function however, is that it doesn't create a block diagonal matrix of arbitrary sizeit only concatenates the numer of arguments you input. In my case, all of the blocks are identical (viz., they are the tribanded matrix, B[1,2,1]), and I want to place N1 of these blocks on the diagonal of the larger matrix. Obviously, I'm not going to manually type in the block N1 times, so I was hoping that maybe someone out there would have a clever solution to this problem. If there are other ways of creating the matrix I'm looking for, that's great too, but it would be neat if I could get this particular method to work. I'm rather new to MATLAB so forgive me if this seems like a dumb question. 



#2
Jun205, 01:36 AM

P: 646

Run a for loop and give it your B matrix. It will create another matrix (say C) with B repeated N1 times and then give this C to blkdiag.
 AI 



#3
Aug3007, 05:41 PM

P: 1

If you have huge matrices, such a loop can be extremely long to run (order n^3, as you have to reallocate memory and copy at each step). As this page is the first to pop in google for "matlab block diagonal", this might be helpful to others:
PierreAndré 



#4
Jul2409, 04:54 AM

P: 22

block diagonal matrices in MATLAB?
Hi,
I have to write a block tridiagonal matrix of dimension 100 in matlab and I tried every thing but it is not working for me. I have a matrix B of dimension 10 which is tridiagonal and 4 on the main diagonal and super diagonal and 6 on the subdiagonal. From this B I have to develop a tridiagonal matrix A of dimension 100 whose leading diagonal is my above matrix B and whose upper and lower diagonals are Matrix I. I will be grateful if anyone can help me how to write it in matlab and especially as a part of Mfile. Thanks mfarooq52003 



#5
Jul2409, 05:43 AM

P: 341

Suppose your matrix is B the block that you want to repeat 100 times, then
mfarooq, try to use the following commands,




#6
Jul2409, 06:08 AM

P: 22

Hi trambolin
Thank you very much for your help and such a prompt response. Regards Farooq 



#7
Jul2409, 06:22 AM

P: 22

Hi,
Now if I have a tridiagonal matrix B with main diagonal 4, upper diagonal 4 and lower diagonal 6 and I have to creat a matirx A with main diagonal the above matrix B, upper and lower diagonals Identity matrix I of the same dimension B, how we can creat that. My actual problem is writing this A in the above mentioned problem. I will be grateful for your help. Cheers Farooq 



#8
Jul2409, 07:05 AM

P: 341

Then you use the same trick over and over again.
to put the B matrices on the diagonal




#9
Jul2409, 10:10 AM

P: 22

Hi Trambolin,
Thank you once again for your help and quick response. It worked this time and my matrix A is now block tridiagonal. I just made the following changes according to my matrix structure, BLI = BL  diag(ones(1,n*qp),p)  diag(1*ones(1,n*qp),p); i.e changed the positive signd to negatives. Thank you very much my friend once again. Regards Farooq 



#10
Nov1309, 09:49 AM

P: 2

Hi,
i have blocktridiagonal matrix A of order n^2 * n^2. all the entries are at the super, main and sub diagonal, that are tridiagonal matrices themselves, of order n*n. i have stored all the entries(matrices) using 3D array. Now i am lost, how to put them back in A as tridiagonals, as A is 2D matrix. secondly, can i avoid 3D arrays. I have to pass A as argument in the triblocksolve algo by Thomas in Matlab. 



#11
Nov1309, 09:52 AM

P: 2

Plz help, its urgent.
Thanks. jave 



#12
Oct1810, 03:00 PM

P: 1

Greetings ~
I am similarly new to Matlab & looking to define a matrix whose blockdiagonal (2x2) matrices are identical, and whose offdiagonal elements are uniform. From the above I think I can sort out how to get the blockdiagonal first, but I can't for the life of me sort how to set all the other elements without altering the (block)diag. elements. Any elegant solutions?! Thank you. Rax 



#13
Oct2010, 03:34 PM

P: 341

First, my apologies to Jave I did not see his post and quite some time past.
Rax, what do you mean by uniform? 



#14
Oct2010, 05:50 PM

P: 32

The matrix P represents probabilities associated with Ising spins.I think it is actually called a rank 4 tensor in 2D (?)  it has NxN 2x2 matrices as its elements that describe the probabilities of finding any given pair of spins aligned or antialigned.
As such, the diagonal 2x2 elements of P have a different probability distribution (viz. P(ii) = [1/2 0; 0 1/2] vs. P(ij) = [ 1/4 1/4 ; 1/4 1/4], for i neq. j). This is just for a uniform probability distribution, but ultimately I will be looking at more complicated distributions, so the more control over this entire matrix I have, the better. At the moment, I've resolved the issue in the following way: L = [ 1/21/4 0; 0 1/21/4]; E = kron( L, eye(n) ); A = 1/4*ones(2*n); F = A + E; for i = 1:n F(i, 2*i1) = 0; F(2*i  1, i) = 0; end Which strikes me as particularly inelegant & unnecessarily verbose (I have heard several times that if you have to use a for loop in matlab, you're probably doing it the wrong way . . .!) but I don't know how to do better. Thanks so much for having a look. All the best, Rax 



#15
Oct2110, 02:20 PM

P: 124

I think this is what you want (replace 10 with your number of rows).
for matrix: 2 1 0 0 0 0 0 0 0 1 2 1 0 0 0 0 0 0 0 1 2 0 0 0 0 0 0 ... use: toeplitz([2,1,zeros(1,10)])  Sorry didn't realize how old this post was  must have been necro'd by someone/ 



#16
Oct2110, 03:08 PM

P: 32

Ah  if you were replying to my post, that's not really what I was trying to get, for N = 2, it would look like this:
0.50 0.00 0.25 0.25 0.00 0.50 0.25 0.25 0.25 0.25 0.50 0.00 0.25 0.25 0.00 0.50 Thank you for having a look! Cheers, Rax 



#17
Oct2110, 04:08 PM

P: 124

Yeah, I was originally responding to the first, then saw yours, then realized you were asking a different question.
Can't think off hand of something super elegant for you. This is best I can do offhand, sorry :( blocks=blkdiag(A,B); background1=k*ones(10,10); background2=blkdiag(k*ones(5,5),k*ones(5,5)) final=blocks+background1+background2; 



#18
Oct2110, 04:17 PM

P: 32

What would be the easiest way to generalize this for arbitrary matrix size? Specifically, how can I use the blkdiag() function without having to manually insert k*ones(n/2, n/2) over and over again?
This is what I was getting at when I was trying to define a matrix of arrays (i.e. a tensor) instead of just a matrix  but I can't figure out how to do this... 


Register to reply 
Related Discussions  
Need help: Proof Nullrank theorem with block matrices  Linear & Abstract Algebra  0  
matrices in matlab  Math & Science Software  2  
the integration block in matlab  Math & Science Software  3  
Matrices in Matlab  Math & Science Software  5  
A.B= m1, B.A=m2, m1 and m2 diagonal matrices  Linear & Abstract Algebra  9 