Why do I receive an error message saying: “??? Index exceeds matrix dimensions. Error in ==> spdiags at 114” when using the SPDIAGS function to create a 2-by-1 sparse matrix from a null matrix in MATLAB?

1 view (last 30 days)
When I execute the following command to create a 2-by-1 sparse matrix from the columns of a null matrix ([]):
spdiags([], 0, 2,1)
I expect MATLAB to return an empty matrix, but I receive the following error message:
??? Index exceeds matrix dimensions.
Error in ==> spdiags at 114
a((len(k)+1):len(k+1),:) = [i i+d(k) B(i+(m>=n)*d(k),k)];

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Jun 2009
This is expected behavior in MATLAB. The SPDIAGS function results in an error message saying: "??? Index exceeds matrix dimensions." if the number of rows in the matrix B [ in the syntax "A = spdiags(B,d,m,n)" ] is less than the length of the diagonal of the sparse matrix to be created. Consider the following example:
A = spdiags([1 2], [0 1], 1, 2) % This is correct syntax as number_of_rows(B) = length_of_diagonal(A)
However, the following code would result in the error message:
A = spdiags([1 2], [0 1], 2, 2) % This is incorrect syntax as number_of_rows(B) < length_of_diagonal(A) which is 2

More Answers (0)

Categories

Find more on Sparse Matrices in Help Center and File Exchange

Products


Release

R14SP1

Community Treasure Hunt

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

Start Hunting!