Every once in a while I need to generate a tridiagonal matrix with replicated elements down the diagonals. Sometimes its nice to generate block tridiagonal matrices of the same form.
2 -1 0 0 0
-1 2 -1 0 0
0 -1 2 -1 0
0 0 -1 2 -1
0 0 0 -1 2
1 1 3 3 0 0
1 1 3 3 0 0
2 2 1 1 3 3
2 2 1 1 3 3
0 0 2 2 1 1
0 0 2 2 1 1
This can of course be easily extended to handle general blocks, so since I saw an interest from a user, this release does that too.
Amd = reshape(1:16,[2,2,4]);
Asub = reshape(101:112,[2,2,3]);
Asup = reshape(201:212,[2,2,3]);
A = blktridiag(Amd,Asub,Asup);
1 3 201 203 0 0 0 0
2 4 202 204 0 0 0 0
101 103 5 7 205 207 0 0
102 104 6 8 206 208 0 0
0 0 105 107 9 11 209 211
0 0 106 108 10 12 210 212
0 0 0 0 109 111 13 15
0 0 0 0 110 112 14 16
I've also seen a request to allow non-square blocks. This too is now accommodated in the latest release.
A = blktridiag([1;2],[3;4],[5;6],4);
1 5 0 0
2 6 0 0
3 1 5 0
4 2 6 0
0 3 1 5
0 4 2 6
0 0 3 1
0 0 4 2
Absolutely useful, especially when solving elliptical PDEs.
Good one! Only thing to add would be the possibility to just pass  for super/sub diagonal instead of zeros, if one only wants the sub/super diagonal, resp.
Thank you, it worked nicely.
Sorry. It was not written to accept a cell array of sparse matrices. It does work if you pass in a single element that is intended to be replicated down the diagonals. So this works of course:
A = sparse([1 0;0 4]);
bigA = blktridiag(A,A,A',10);
And blktridiag would work if MATLAB supported 3-d sparse arrays.
blkdiag works for you because you can pass in a comma separated list of elements. So an easy fix is for you to use blkdiag to build the main diagonal. Then use blkdiag to build the sub and super diagonal arrays. Shift them over as necessary by padding with zeros, and then just add the three arrays together. No problem.
Hi, would it be possible to use blktridiag with cell arrays of sparse matrices? I have a 3d volume (nxmxp) and I must create a tridiagonal matrice of size (nxmxp)x(nxmxp) where the matrices (nxm)x(nxm) for all the slices p of the volume are on the main diagonal. The sub and sup diagonals are matrices also of size (nxm)x(nxm) representing the relationship between neighboring slices.
W11 W12 [ ] [ ]
W21 W22 W23 [ ]
[ ] W32 W33 W34
[ ] [ ] W43 W44
Where W21 is the same as W12' (transpose).
Since Matlab doesn't support 3d sparse matrices, I thought I could use a cell array. The function blkdiag from Matlab works with my cell array, but I am missing the sub and sup diagonals.
I did it in other way. I defined Amdb (A main diag. boundary blocks) in your code and then applied this:
It works and now I will try to go further with the solution of the examined problem. Thank you and regards!
Thank you for the quick and complete answer. Unfortunately, I'm not familiar with a "repmat" and how to replicate only the middle blocks and replace the first and the last one.
I tried this:
v = repmat(AmdF(1),n,1);
v = repmat(Amd(:),n,1);
v = repmat(AmdL(n),n,1);
but of course it doesn't work....
Have you got a clue how to call it?
Thank you in advance and appreciation again for the code!!!
Of course it is possible, since you can supply the individual blocks along the main diagonal. Since the diagonal blocks can be supplied as a pxqxn array in Amd, use repmat to replicate the middle blocks, then replace the first and last blocks in Amd as you wish. Then its just a call to blktridiag.
can please help me to find out if it's possible, using "blktridiag", to generate a block tridiagonal matrix A, whose main diagonal (Amd) to starts and ends with different copies of Amd. I.e. AmdFirst and AmdLast~= Amd...
[AmdF Asup 0 0 0;
Asub Amd Asup 0 0;
0 Asub Amd Asup 0;
0 0 Asub Amd Asup;
0 0 0 Asub AmdL]
I hope that the explanation was clear enought! Thank you in advance!
It means you have not placed the function in a directory on your search path. Do NOT put it in the matlab toolbox directories, but in some other directory that you should reserve for your own functions. Then make sure that directory is on the search path. Use the functions addpath, savepath and pathtool to set up your search path.
Hello, can you help with this error:??? Undefined function or method 'blktridiag' for input arguments of type
Probably, it is a stupid question, but I really need help! Thank you in advance!
Nice work! Thanks so much!
Good example for making a sparse matrix.
Many thank for the function, it work very well for square blocks.
Can you have any idea for tridiagonal matrix with not square blocks?
One again many thanks
Great example of the best way to create a sparse matrix. Nice demo (including a demo is very important in a usable submission).
One minor comment: your submission as of May 11 includes ".DS_Store" files in each folder, which only tell a Mac how to size the window when viewing the folders. Those files are visible to the Windows and Unix/Linux users. They can be deleted.
Very useful code....
Could you please show us the code for the extension too? Thanks in advance.
Wonderful code, and very useful. Thanks John
Thank you John.
Nice work John. Very usefull indeed. This is a very useful code, especially CFD.
Update to remove the .DS_Store files in the zip file. (My thanks to Tim, and of course, to the gipper.)
Cleaned up the code a bit, removed replicated code blocks and a spare call to ndgrid. Also special cased scalar tridiagonals using spdiags. (Thanks to Jos for pointing these items out.)
Added test subdirectory for Select nom
Added the ability to handle general, distinct blocks, also added demos and a screenshot.
Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.