Search code examples
matlabsyntax-errorsparse-matrixsubscript

Error for sparse matrix generated by gallery: subscript indices must either be real positive integers or logicals


I generated a sparse matrix by:

A = full(gallery('tridiag',n,1,4,1));

When I try to generate the eigenvectors for this function via:

eig(A)

I receive the standard the error "Subscript indices must either be real positive integers or logicals." I read through Subscript indices must either be real positive integers or logicals, generic solution, but cannot figure out how to correct the problem. Is this simply due to the way I have created the sparse matrix, i.e. with the gallery command? If so, how can I create the matrix without this issue?

Error output

I was able to find the following:

They use different algorithms, tailored to different problems and different goals.

eig is a good, fast, general use eigenvalue/vector solver. It is appropriate for use when your matrix is of a realistic size that fits well in memory, and when you need all of the eigenvalues/vectors. Sparse matrices do not work at all in eig.

Eigs is a solver that is more appropriate for when you need only a limited subset of the eigenvalues/vectors. Here the matrix is often stored in sparse format, because as a full matrix, it would take up too much memory to store. It appears that eigs is based on ARPACK.

If you truly are asking for specifics on the actual algorithms, this is a question that is clearly inappropriate for this site. Sit down with a copy of "Matrix Computations", or better yet, read the pair of references listed in the doc for eigs. From What is the difference between 'eig' and 'eigs'?


Solution

  • The reported error suggested that eig was treated as a variable. In that case one should clear the workspace (clear) and try eig again. That would also immediately evident if one used: which eig in which case MATLAB would return: eig is a variable. (Instead of a built-in method)

    If that does not work the next obvious things are to use regenerate MATLAB's preferences and then rehash the toolbox cache in case any of the two has become corrupted from an external problem or by accident from the user.