0

I'm working with large sparse matrices and small sparse matrices.

Now, the eig command doesn't work with sparse matrices (when using sprand) so I must use eigs.

But I have a script that needs to use both depending on what matrix I'm using. I could have two identical scripts, one with eig the other eigs (and 5,6 <=> N-1,N) but I'd like to know how to combat this.

Here is the start of my script.

f=eigs(A);  % was eig    
figure(1)
semilogy(res) %

convfact = abs(f(5))/abs(f(6));   % 5,6 was N-1 and N respectively
hold on 
semilogy(convfact.^(1:k),'r--')

I'd also like to point out, for when I'm using the eig command, as the comment in the code says, the 5 and 6 was the N-1 and N. But obviously MATLAB can't find these values on a 0000x0000 matrix so I must use the 5th and 6th that eigs provides.

Any advice on solving this will be great, thanks.

chappjc
  • 30,359
  • 6
  • 75
  • 132
  • Not sure what exactly you are asking. But have you tried using an `if` statement to test what kind of matrix you have, and then use the corresponding command? From your title it seems like this can also be dealt with using a `try-catch` block but that is probably not the best way to go. – Dennis Jaheruddin Feb 12 '14 at 11:23

1 Answers1

1

What's wrong with a simple if/else block?

if issparse(A)
    f = eigs(A);
else
    f = eig(A);
end
sebastian
  • 9,526
  • 26
  • 54