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?

2 Ansichten (letzte 30 Tage)
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)];

Akzeptierte Antwort

MathWorks Support Team
MathWorks Support Team am 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

Weitere Antworten (0)

Kategorien

Mehr zu Sparse Matrices finden Sie in Help Center und File Exchange

Produkte


Version

R14SP1

Community Treasure Hunt

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

Start Hunting!

Translated by