MATLAB Answers

Sparse Matrix with Non-positive Indices

2 views (last 30 days)
David Mwangi
David Mwangi on 11 Jun 2019
Edited: John D'Errico on 11 Jun 2019
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 Comment

Jan on 11 Jun 2019
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.

Sign in to comment.

Answers (1)

John D'Errico
John D'Errico on 11 Jun 2019
Edited: John D'Errico on 11 Jun 2019
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.


Sign in to comment.

Sign in to answer this question.