Register to reply

Block diagonal matrices in MATLAB?

by NoOne79
Tags: block, diagonal, matlab, matrices
Share this thread:
Oct21-10, 05:08 PM
P: 124
Quote Quote by raxAdaam View Post
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?
Try playing with something like...
Oct22-10, 08:43 AM
P: 341
rax,it is the same as the block diagonal. So for your last example,

B = 0.5*eye(4) + 0.25*(kron([0 1;1 0],ones(2))

will do. Basically it is the application of the same idea, you build from the smallest structure to the bigger one. Of course, there are shorter ways to do it. But it would be an overkill for these sizes.
Oct22-10, 09:49 AM
P: 32
Quote Quote by trambolin View Post
But it would be an overkill for these sizes.
I'm just stepping out to write an exam (joy), so haven't had a chance to run this yet (hard to visualize before I do!) - but the size will grow substantially N will be on the order 100s-1000s (I hope not larger) not including the factor of 2 . . . would this change your approach at all?

Thanks very much for this, both of you - it's really helpful in terms of understanding how Matlab works. Don't know how you figured it out yourself!


Oct24-10, 04:36 AM
P: 341
Yes, I guess it would change the approach but I don't think that it would be something that slight modifications can not fix. When you are done, give us the actual 100-1000 matrix structure and we can figure it out together. (I am not sure what you want on the sup and super diagonals)
Oct24-10, 11:23 AM
P: 124
Quote Quote by raxAdaam View Post
Thanks very much for this, both of you - it's really helpful in terms of understanding how Matlab works. Don't know how you figured it out yourself!
I've found the Octave reference manual pretty useful. It doesn't give many examples or how-to's, but you can flip to a section and get a pretty good idea what tools are available.
Oct30-10, 01:19 PM
P: 32
So the code is running alright for the time being, but apparently draws on too much memory for > 2000 x 2000 (ish).

I'm working on an in-between step now, not requires me to call on this code, run it for several values of one of the variables, generate a graph with title, axes, grid etc and save it to a specific file.

I've figured out how to do everything except the last step (viz. saving the generated figure to a *specific* file that is not simply the matlab directory). Would I simply include a change director "cd - " command *in* the .m file to do this?

Also, is there a more efficient way of creating a vector of the outputs of my function than running a for loop? i.e. is it possible to write a .m file that takes a range of values, with a specified increment and will calculate the output for each of those values *without* using a for loop?

Again (&again!) thanks for the help. I've spent so much time searching around on line to no avail.


Oct31-10, 06:20 PM
P: 341
Well if you are not doing complicated stuff, then, you can vectorize your variables, and send it to your function as vectors instead of computing one by one. it really depends on what you are trying to do.
Nov3-11, 09:54 AM
P: 3

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.
Nov8-11, 04:56 AM
P: 3
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);
Dec29-11, 05:07 AM
P: 2
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.
Oct29-12, 09:41 AM
P: 1
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!

Register to reply

Related Discussions
Need help: Proof Null-rank 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