MATLAB: Mldivide with sparse matrix

linear systemmldividesparsesparse and full matrix

I need to solve large sparse system of linear equations A*x=b. A is a sparse matrix, but b is vector which has only few zero entries. The question is, regarding memory consumption, when I call
when A is sparse matrix and b is full vector, will matlab internally transform A into full matrix and solve the system, or handle it like sparse.
Thanks! K

Best Answer

The following speed comparison is pretty good evidence that only the type of A (sparse or full) dictates how things will be handled,
>> N=6000; A=speye(N); b=rand(N,1); bsp=sparse(b); Af=full(A);
>> tic;A\b;toc
Elapsed time is 0.000316 seconds.
>> tic;A\bsp;toc
Elapsed time is 0.000301 seconds.
>> tic;Af\b;toc
Elapsed time is 0.071560 seconds.
>> tic;Af\bsp;toc
Elapsed time is 0.030398 seconds.
In principle, though, it wouldn't matter. You could have still converted b to sparse type, no matter how dense it actually is, if that's what was needed to force a sparse algorithm to be used. The memory penalty is negligible if b is just a vector.