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 次查看(过去 30 天)
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)];

采纳的回答

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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Sparse Matrices 的更多信息

产品


版本

R14SP1

Community Treasure Hunt

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

Start Hunting!

Translated by