| New Reply |
block diagonal matrices in MATLAB? |
Share Thread |
| Oct21-10, 04:17 PM | #18 |
|
|
block diagonal matrices in MATLAB?
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... |
| Oct21-10, 05:08 PM | #19 |
|
|
A=-k*ones(n,n) kron(eye(M),A) |
| Oct22-10, 08:43 AM | #20 |
|
|
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 | #21 |
|
|
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! Cheers, Rax |
| Oct24-10, 04:36 AM | #22 |
|
|
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 | #23 |
|
|
|
| Oct30-10, 01:19 PM | #24 |
|
|
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. Cheers, Rax |
| Oct31-10, 06:20 PM | #25 |
|
|
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 | #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. |
| Nov8-11, 04:56 AM | #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". |
| Dec29-11, 05:07 AM | #28 |
|
|
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 | #29 |
|
|
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 |
| New Reply |
Similar discussions for: block diagonal matrices in MATLAB?
|
||||
| Thread | Forum | Replies | ||
| 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 | ||