Clear Filters
Clear Filters

Error with spdiags size dimensions in pentadiagonal matrixes

9 views (last 30 days)
I'm trying to create a sparse pentadiagonal matrix for a derivation discretization problem I'm working on. This matrix sould have the diagonals [1, -8, 0, 8, -1], with the value 0 being the one centered on the main diagonal.
I'm getting the error
Error using spdiags
For the syntax spdiags(B,d,m,n), the size of B must be min(m,n)-by-length(d).
Error in burnfile (line 15)
D1 = spdiags([1 -8 0 8 -1],-2:2,nx,nx);
It boggles me because if I try to paste in the tridiagonal example in the spdiagonal documentation page, it gives me the same error instead of working (having cleared all variables and on a clean notebook).
I'm working with Matlab R2022b, in case that's relevant.
The code I'm using is as follows:
nx = 1000;
D1 = spdiags([1 -8 0 8 1],-2:2,nx,nx);
full(D1)
ans = 1000x1000
0 8 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -8 0 8 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 -8 0 8 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 -8 0 8 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 -8 0 8 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 -8 0 8 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 -8 0 8 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 -8 0 8 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 -8 0 8 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 -8 0 8 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
  4 Comments
Matt J
Matt J on 9 May 2024
Edited: Matt J on 9 May 2024
Sorry, but why can't it be tested in a previous version? Also I don't know what you mean by "see above"
Your code has been run in your post using the forum's Matlab engine. That's what has produced the output there. However, only R2024a is available for this:
Torsten
Torsten on 9 May 2024
Edited: Torsten on 9 May 2024
@Matt J answered your first question. With "see above", I mean "look at your question: your code can be executed and does not throw an error in R2024a".

Sign in to comment.

Accepted Answer

Matt J
Matt J on 9 May 2024
Edited: Matt J on 9 May 2024
Does anyone know if spdiags has undergone any significant updates between versions these 2 years?
Yes, support for the syntax you are using is relatively new. The old way is,
nx = 1000;
e=ones(nx,1);
D1 = spdiags(e*[1 -8 0 8 1],-2:2,nx,nx);
full(D1)
ans = 1000x1000
0 8 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -8 0 8 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 -8 0 8 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 -8 0 8 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 -8 0 8 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 -8 0 8 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 -8 0 8 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 -8 0 8 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 -8 0 8 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 -8 0 8 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
  6 Comments
Steven Lord
Steven Lord on 10 May 2024
FYI there's an entry in the Release Notes for this functionality in spdiags. It was introduced in release R2024a.
To see older documentation, click on the Support link at the top of many MathWorks webpages (including this one.) On the right side of the page, under the release number of the current release (indicating the Help Center is showing the documentation for the current release) click on Other Releases. You can select from one of last 5 years worth of releases (10 releases.) For example this is the documentation page for spdiags in release R2022b and this is the documentation page for spdiags in the current release. They look at a quick skim to be very similar until you get to the section for the Bin input argument and the Version History section.
Pol
Pol on 13 May 2024
Ahh! That makes a lot of sense, thanks for the detailed explanation, Steven. I did try to find the previous documentation as Torsten pointed out I should have done, of course, but I just sticked to Google and it just wouldn't show up. Thanks! I will always check on the appropiate documentation from now on!

Sign in to comment.

More Answers (0)

Categories

Find more on Operating on Diagonal Matrices in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!