Suppose $A$ is a $n\times n$ dense matrix and you have to solve $Ax_i = b_i$, $i=1\dots m$. If $m$ is big enough then there is nothing wrong in
V = inv(A);
...
x = V*b;
Flops are $O(n^3)$ for inv(A)
and $O(n^2)$ for V*b
, therefore in order to determine the break-even value for $m$ some experimentation is needed...
>> n = 5000;
>> A = randn(n,n);
>> x = randn(n,1);
>> b = A*x;
>> rcond(A)
ans =
1.3837e-06
>> tic, xm = A\b; toc
Elapsed time is 1.907102 seconds.
>> tic, [L,U] = lu(A); toc
Elapsed time is 1.818247 seconds.
>> tic, xl = U\(L\b); toc
Elapsed time is 0.399051 seconds.
>> tic, [L,U,p] = lu(A,'vector'); toc
Elapsed time is 1.581756 seconds.
>> tic, xp = U\(L\b(p)); toc
Elapsed time is 0.060203 seconds.
>> tic, V=inv(A); toc
Elapsed time is 7.614582 seconds.
>> tic, xv = V*b; toc
Elapsed time is 0.011499 seconds.
>> [norm(xm-x), norm(xp-x), norm(xl-x), norm(xv-x)] ./ norm(x)
ans =
1.0e-11 *
0.1912 0.1912 0.1912 0.6183
In this trivial example $A^{-1}$ pre-computation is better than $LU$ forward and backward solution for $m>125$.
Some notes
For stability and error analysis please see the comments to this different answer, especially the one by VictorLiu.
The proposed timings are not "scientific" at all, but are meant to show that the approach proposed in the answer by Milind R, while it makes perfect sense if implemented in C or Fortran by calling relevant LAPACK and BLAS subroutines, may prove not so effective in Matlab, even for $m\ll n$.
Timing were performed with Matlab R2011b on a 12 core computer with a fairly constant UNIX load average of 5; best tic, toc
time of three probes.