# How to compute all the eigenvalues of a large sparse matrix using matlab?

In matlab, there are 2 commands named "eig" for full matrices and "eigs" for sparse matrices to compute eigenvalues of a matrix. And eig(A) computes all the eigenvalues of a full matrix and eigs(A) computes the 6 largest magnitude eigenvalues of matrix A. If we want to compute all the eigenvalues of a sparse matrix, so we must convert the matrix to full type, i.e., using eig(full(A)) when A is sparse. But this will fail becaues of CPU memory. My question is that if I want to compute all the eigenvalues of a large sparse matrix, say (matrix size is 10000). How to implement this? I know we should not compute all the eigenvalues of a large sparse matrix. But sometimes, in Krylove subspace iterative methods, we need to plot the spectral distributions of a preconditioned matrix to investigate the convergence rate. so I need plot the spectral distributions. How to do that? Thanks very much.

Below is my random example and fails. And the matrix is from Poisson equation using centered difference in 2D:

clc;clear;
n=100;
A = gallery('poisson',n);% system size is n*n
R = ichol(A);
P = R'*R;%  construct preconditioner
%   consider the eigenvalue distribution of  preconditioned matrix inv(P)*A
a = eig(full(A),full(P));
plot(real(a),imag(a))


"Get more RAM" may be one of your best options. :) Prices are reasonably low right now, and it's one of the best upgrades you can gift your computer anyway. 10k x 10k is borderline but still doable on modern computers: that matrix takes $$10^4 \times 10^4 \times 8$$ bytes, that is, 760 MiB. On my laptop that code runs without problems.
Another option is running your code with a smaller $$n$$. It looks like you are running that computation to get insight on how your preconditioner works, not numbers. You can probably get the same insight with a 5k x 5k matrix, or a 2k x 2k matrix. And those will probably fit in your memory.