MATLAB Answers


Sparse Matrix with Non-positive Indices

David Mwangi さんによって質問されました 2019 年 6 月 11 日
最新アクティビティ John D'Errico
さんによって 編集されました 2019 年 6 月 11 日
I was making a sparse matrix from a full matrix but the results of the sparse matrix has some indices which are negative. What could be the reason?
I have always done sparse matrix with no problem but am not sure of this scenario. Thanks in advance for your assistance.

  1 件のコメント

2019 年 6 月 11 日
Because indices cannot be negative in Matlab, your method to determine these indices must contain an error. So please mention, how you think you determine the indices. Post some code which replicates the observed behavior.

サインイン to comment.

1 件の回答

John D'Errico
回答者: John D'Errico
2019 年 6 月 11 日
編集済み: John D'Errico
2019 年 6 月 11 日

What you say makes no sense. If you have a full matrix already, and you are turning that into a sparse matrix, then you would just use the function sparse to make it sparse. As such, the indices of that matrix will not be negative, since the matrix already exists. So that means you are not doing what you said you are doing.
Instead, it looks like you are creating a matrix from some formula for the elements of a matrix, and at the boundary of that problem, your formula creates some zero or negative indices. Now you have a situation where you are confused, since MATLAB does not allow matrices with a negative or zero index. So MATLAB generates an error, telling you that you cannot have zero or negative indices into a matrix.
That means in turn, you need to understand what to do at that boundary. This typically involves the formulation of a boundary condition, so a different formula for the problem at the boundary. That is a classic issue in mathematics when you are solving problems of this sort, but it is not just an issue of MATLAB. It comes into play whenever you are solving any such problem in any language, because a matrix is a finite thing. It has a boundary. In fact, this happens even for more general mathematical problems, thus PDEs, boundary value problems. You need to understand how to formulate that boundary in terms of mathematics.
Finally, it may sometimes force you to use an offset index, recognizing that MATLAB uses an index origin of ONE for its matrices.

  0 件のコメント

サインイン to comment.

Translated by