<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/157000</link>
    <title>MATLAB Central Newsreader - Inverse of a Matrix</title>
    <description>Feed for thread: Inverse of a Matrix</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2012 by MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Tue, 02 Oct 2007 04:15:40 -0400</pubDate>
      <title>Inverse of a Matrix</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/157000#394707</link>
      <author>Ravi </author>
      <description>Are there any methods available for finding approximate&lt;br&gt;
inverse for nearly singular matrices using MATLAB? Is there&lt;br&gt;
a way to avoid this?</description>
    </item>
    <item>
      <pubDate>Tue, 02 Oct 2007 08:06:41 -0400</pubDate>
      <title>Re: Inverse of a Matrix</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/157000#394717</link>
      <author>Bjorn Gustavsson</author>
      <description>&quot;Ravi &quot; &amp;lt;vioravis.nospam@gmail.com&amp;gt; wrote in message&lt;br&gt;
&amp;lt;fdsglc$6b7$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Are there any methods available for finding approximate&lt;br&gt;
&amp;gt; inverse for nearly singular matrices using MATLAB? Is there&lt;br&gt;
&amp;gt; a way to avoid this?&lt;br&gt;
&amp;gt; &lt;br&gt;
You could try the svd approach. Then you can do damping and&lt;br&gt;
truncation directly on the singular values.&lt;br&gt;
&lt;br&gt;
HTH,&lt;br&gt;
Bjoern</description>
    </item>
    <item>
      <pubDate>Tue, 02 Oct 2007 09:09:20 -0400</pubDate>
      <title>Re: Inverse of a Matrix</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/157000#394724</link>
      <author>Duane Hanselman</author>
      <description>&quot;Ravi &quot; &amp;lt;vioravis.nospam@gmail.com&amp;gt; wrote in message&lt;br&gt;
&amp;lt;fdsglc$6b7$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Are there any methods available for finding approximate&lt;br&gt;
&amp;gt; inverse for nearly singular matrices using MATLAB? Is there&lt;br&gt;
&amp;gt; a way to avoid this?&lt;br&gt;
&lt;br&gt;
What do you need the inverse of a nearly singular matrix&lt;br&gt;
for? You cannot expect to get a good inverse from a bad&lt;br&gt;
matrix. The condition number of the inverse will not&lt;br&gt;
miraculously be better than the original matrix. Garbage in,&lt;br&gt;
garbage out applies in this case. There is no way around it.&lt;br&gt;
All you can expect from MATLAB is an inverse whose&lt;br&gt;
properties are not significantly worse than the original&lt;br&gt;
matrix. There are very few reasons for ever computing an&lt;br&gt;
inverse.</description>
    </item>
    <item>
      <pubDate>Tue, 02 Oct 2007 09:40:50 -0400</pubDate>
      <title>Re: Inverse of a Matrix</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/157000#394726</link>
      <author>John D'Errico</author>
      <description>&quot;Ravi &quot; &amp;lt;vioravis.nospam@gmail.com&amp;gt; wrote in message &amp;lt;fdsglc$6b7&lt;br&gt;
$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Are there any methods available for finding approximate&lt;br&gt;
&amp;gt; inverse for nearly singular matrices using MATLAB? Is there&lt;br&gt;
&amp;gt; a way to avoid this?&lt;br&gt;
&lt;br&gt;
As Duane points out, why do you want the inverse at all?&lt;br&gt;
&lt;br&gt;
If you are using it to solve a system of equations, then&lt;br&gt;
its a very poor choice.&lt;br&gt;
&lt;br&gt;
A pseudo-inverse, as provided by pinv will be a good&lt;br&gt;
choice however. It uses the svd to survive the singularity,&lt;br&gt;
and your result will be as stable as is numerically possible.&lt;br&gt;
For example:&lt;br&gt;
&lt;br&gt;
A = ones(2);&lt;br&gt;
A(1,1) = 1+eps;&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
inv(A)&lt;br&gt;
Warning: Matrix is close to singular or badly scaled.&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Results may be inaccurate. RCOND = 5.551115e-17.&lt;br&gt;
ans =&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;4.5036e+15  -4.5036e+15&lt;br&gt;
&amp;nbsp;&amp;nbsp;-4.5036e+15   4.5036e+15&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
pinv(A)&lt;br&gt;
ans =&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0.25         0.25&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0.25         0.25&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
A*pinv(A)&lt;br&gt;
ans =&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0.5          0.5&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0.5          0.5&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
A*inv(A)&lt;br&gt;
Warning: Matrix is close to singular or badly scaled.&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Results may be inaccurate. RCOND = 5.551115e-17.&lt;br&gt;
ans =&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;1     0&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0     1&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
However, perturb A only by a tiny bit and see how&lt;br&gt;
much the inverse changes. &lt;br&gt;
&lt;br&gt;
B = A;&lt;br&gt;
B(2,2) = 1-eps;&lt;br&gt;
&lt;br&gt;
inv(B)&lt;br&gt;
Warning: Matrix is singular to working precision.&lt;br&gt;
ans =&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;Inf   Inf&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;Inf   Inf&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Or instead, try this:&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
B*inv(A)&lt;br&gt;
Warning: Matrix is close to singular or badly scaled.&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Results may be inaccurate. RCOND = 5.551115e-17.&lt;br&gt;
ans =&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;1            0&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;1            0&lt;br&gt;
&amp;nbsp;&lt;br&gt;
&lt;br&gt;
Whereas pinv(B) is still the same stable result.&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
pinv(B)&lt;br&gt;
ans =&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0.25         0.25&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0.25         0.25&lt;br&gt;
&lt;br&gt;
HTH,&lt;br&gt;
John</description>
    </item>
    <item>
      <pubDate>Tue, 02 Oct 2007 19:54:47 -0400</pubDate>
      <title>Re: Inverse of a Matrix</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/157000#394854</link>
      <author>Ravi </author>
      <description>John and others,&lt;br&gt;
&lt;br&gt;
Thanks for your replies. I am not in a position to avoid&lt;br&gt;
using matrix inversion. pinv really seems to give some&lt;br&gt;
erratic answers for some of the test cases I have. I would&lt;br&gt;
like to if there is a method to calculate the inverse using&lt;br&gt;
Cholesky decomposition and if so, whether it would be better&lt;br&gt;
than the inv operation in MATLAB?&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&quot;John D'Errico&quot; &amp;lt;woodchips@rochester.rr.com&amp;gt; wrote in&lt;br&gt;
message &amp;lt;fdt3n2$kej$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &quot;Ravi &quot; &amp;lt;vioravis.nospam@gmail.com&amp;gt; wrote in message&lt;br&gt;
&amp;lt;fdsglc$6b7&lt;br&gt;
&amp;gt; $1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; Are there any methods available for finding approximate&lt;br&gt;
&amp;gt; &amp;gt; inverse for nearly singular matrices using MATLAB? Is there&lt;br&gt;
&amp;gt; &amp;gt; a way to avoid this?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; As Duane points out, why do you want the inverse at all?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; If you are using it to solve a system of equations, then&lt;br&gt;
&amp;gt; its a very poor choice.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; A pseudo-inverse, as provided by pinv will be a good&lt;br&gt;
&amp;gt; choice however. It uses the svd to survive the singularity,&lt;br&gt;
&amp;gt; and your result will be as stable as is numerically possible.&lt;br&gt;
&amp;gt; For example:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; A = ones(2);&lt;br&gt;
&amp;gt; A(1,1) = 1+eps;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; inv(A)&lt;br&gt;
&amp;gt; Warning: Matrix is close to singular or badly scaled.&lt;br&gt;
&amp;gt;          Results may be inaccurate. RCOND = 5.551115e-17.&lt;br&gt;
&amp;gt; ans =&lt;br&gt;
&amp;gt;    4.5036e+15  -4.5036e+15&lt;br&gt;
&amp;gt;   -4.5036e+15   4.5036e+15&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; pinv(A)&lt;br&gt;
&amp;gt; ans =&lt;br&gt;
&amp;gt;          0.25         0.25&lt;br&gt;
&amp;gt;          0.25         0.25&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; A*pinv(A)&lt;br&gt;
&amp;gt; ans =&lt;br&gt;
&amp;gt;           0.5          0.5&lt;br&gt;
&amp;gt;           0.5          0.5&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; A*inv(A)&lt;br&gt;
&amp;gt; Warning: Matrix is close to singular or badly scaled.&lt;br&gt;
&amp;gt;          Results may be inaccurate. RCOND = 5.551115e-17.&lt;br&gt;
&amp;gt; ans =&lt;br&gt;
&amp;gt;      1     0&lt;br&gt;
&amp;gt;      0     1&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; However, perturb A only by a tiny bit and see how&lt;br&gt;
&amp;gt; much the inverse changes. &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; B = A;&lt;br&gt;
&amp;gt; B(2,2) = 1-eps;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; inv(B)&lt;br&gt;
&amp;gt; Warning: Matrix is singular to working precision.&lt;br&gt;
&amp;gt; ans =&lt;br&gt;
&amp;gt;    Inf   Inf&lt;br&gt;
&amp;gt;    Inf   Inf&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Or instead, try this:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; B*inv(A)&lt;br&gt;
&amp;gt; Warning: Matrix is close to singular or badly scaled.&lt;br&gt;
&amp;gt;          Results may be inaccurate. RCOND = 5.551115e-17.&lt;br&gt;
&amp;gt; ans =&lt;br&gt;
&amp;gt;             1            0&lt;br&gt;
&amp;gt;             1            0&lt;br&gt;
&amp;gt;  &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Whereas pinv(B) is still the same stable result.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; pinv(B)&lt;br&gt;
&amp;gt; ans =&lt;br&gt;
&amp;gt;          0.25         0.25&lt;br&gt;
&amp;gt;          0.25         0.25&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; HTH,&lt;br&gt;
&amp;gt; John&lt;br&gt;
&amp;gt; </description>
    </item>
    <item>
      <pubDate>Tue, 02 Oct 2007 20:52:31 -0400</pubDate>
      <title>Re: Inverse of a Matrix</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/157000#394858</link>
      <author>roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)</author>
      <description>In article &amp;lt;fdt1s0$l5f$1@fred.mathworks.com&amp;gt;,&lt;br&gt;
Duane Hanselman &amp;lt;masteringmatlab@yahoo.spam.com&amp;gt; wrote:&lt;br&gt;
&lt;br&gt;
&amp;gt;There are very few reasons for ever computing an&lt;br&gt;
&amp;gt;inverse.&lt;br&gt;
&lt;br&gt;
I've been wondering about that statement, which I have seen several&lt;br&gt;
people post.&lt;br&gt;
&lt;br&gt;
I am working with a function &lt;br&gt;
&lt;br&gt;
Pij = g( Dij * inv(f1(m1,m2)) * Dij' + log(det(f1(m1,m2))),&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Dij * inv(f2(m1,m2)) * Dij' + log(det(f2(m1,m2))) )&lt;br&gt;
&lt;br&gt;
Where&lt;br&gt;
&amp;nbsp;Dij is ([m1;m2](i,:) - [m1;m2](j,:)) -- i.e., a difference vector&lt;br&gt;
&amp;nbsp;f1(m1,m2) is a matrix function in the covariances of m1 and m2&lt;br&gt;
&amp;nbsp;f2(m1,m2) is a matrix function closely related to f1&lt;br&gt;
&lt;br&gt;
As Dij is a vector, Dij * inv(f1(m1,m2)) * Dij' is a scalar,&lt;br&gt;
as is Dij * inv(f2(m1,m2)) * Dij'; g() calculates a scalar result.&lt;br&gt;
&lt;br&gt;
Hence, P is a (symmetric) square matrix of scalar results, &lt;br&gt;
each of which involves a vector-matrix-vector calculation.&lt;br&gt;
&lt;br&gt;
I found a matrix formulation that allows me to calculate&lt;br&gt;
an entire column of P at one time. However, because I need a 2D&lt;br&gt;
matrix output and each element of that output involves a vector&lt;br&gt;
matrix computation, in order to calculate everything in one shot,&lt;br&gt;
I would need Matlab to do a 3-dimensional matrix multiplication,&lt;br&gt;
which it will not do using the built-in operators.&lt;br&gt;
&lt;br&gt;
So what then do I do if &quot;there are very few reasons for ever&lt;br&gt;
computing an inverse&quot;? I can reformulate the terms with&lt;br&gt;
Dij / f1(m1,m2) * Dij' and Dij / f2(m1,m2) * Dij'&lt;br&gt;
but P can be (for example) 1545 x 1545, so if I did that&lt;br&gt;
reformulation together with the column-at-a-time trick,&lt;br&gt;
would I not be essentially re-doing the logical equivilent of&lt;br&gt;
inverting f1(m1,m2) and f2(m1,m2) each 1545 times? Would that not&lt;br&gt;
be much *much* slower than my present stategy of inverting f1(m1,m2)&lt;br&gt;
and f2(m1,m2) once each?&lt;br&gt;
&lt;br&gt;
Already, working with the 1545 x 1545 matrix is slow. When I was&lt;br&gt;
proceeding an element at a time, vector-matrix-vector, the calculation&lt;br&gt;
of P was taking 65 seconds each time, having pre-calculated the inverse.&lt;br&gt;
The column at a time formulation was barely faster (62 seconds)&lt;br&gt;
until I found that allowing it to do a full matrix multiply and&lt;br&gt;
then taking the diag() was noticably faster -- and then found that&lt;br&gt;
sum(Dij * inf(f1(m1,m2)) .* Dij, 2)  was faster still. I now have it&lt;br&gt;
down to less than 2 seconds per iteration.&lt;br&gt;
&lt;br&gt;
Isn't it the case that if I use sum(Dij / f1(m1,m2) .* Dij, 2) for&lt;br&gt;
each column that I would see a substantial slowdown in the calculation?&lt;br&gt;
Is it worth it? Or is &quot;substantial re-use of the result of the logical&lt;br&gt;
division&quot; one of the &quot;few reasons&quot; that is considered to justify&lt;br&gt;
taking the inverse?&lt;br&gt;
-- &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&quot;I was very young in those days, but I was also rather dim.&quot;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;-- Christopher Priest</description>
    </item>
    <item>
      <pubDate>Tue, 02 Oct 2007 21:17:30 -0400</pubDate>
      <title>Re: Inverse of a Matrix</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/157000#394860</link>
      <author>John D'Errico</author>
      <description>&quot;Ravi &quot; &amp;lt;vioravis.nospam@gmail.com&amp;gt; wrote in message &amp;lt;fdu7m7$ijo&lt;br&gt;
$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; John and others,&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Thanks for your replies. I am not in a position to avoid&lt;br&gt;
&amp;gt; using matrix inversion. pinv really seems to give some&lt;br&gt;
&amp;gt; erratic answers for some of the test cases I have. I would&lt;br&gt;
&amp;gt; like to if there is a method to calculate the inverse using&lt;br&gt;
&amp;gt; Cholesky decomposition and if so, whether it would be better&lt;br&gt;
&amp;gt; than the inv operation in MATLAB?&lt;br&gt;
&lt;br&gt;
Can you explain how you perceive pinv is giving&lt;br&gt;
erratic results? An example would be useful.&lt;br&gt;
&lt;br&gt;
A true matrix inverse should be far more erratic&lt;br&gt;
than pinv. And unless your matrix is symmetric&lt;br&gt;
positive definite, cholesky is not an option.&lt;br&gt;
&lt;br&gt;
John</description>
    </item>
    <item>
      <pubDate>Wed, 03 Oct 2007 00:51:38 -0400</pubDate>
      <title>Re: Inverse of a Matrix</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/157000#394873</link>
      <author>Tim Davis</author>
      <description>roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in &lt;br&gt;
...&lt;br&gt;
&amp;gt; I am working with a function &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Pij = g( Dij * inv(f1(m1,m2)) * Dij' + log(det(f1(m1,m2))),&lt;br&gt;
&amp;gt;          Dij * inv(f2(m1,m2)) * Dij' + log(det(f2(m1,m2))) )&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; Where&lt;br&gt;
&amp;gt;  Dij is ([m1;m2](i,:) - [m1;m2](j,:)) -- i.e., a&lt;br&gt;
difference vector&lt;br&gt;
&amp;gt;  f1(m1,m2) is a matrix function in the covariances of m1&lt;br&gt;
and m2&lt;br&gt;
&amp;gt;  f2(m1,m2) is a matrix function closely related to f1&lt;br&gt;
&lt;br&gt;
What you want to do is factorize the matrix A = f1(m1,m2)&lt;br&gt;
once and then reuse the factorization lots of times.  This&lt;br&gt;
isn't the case of &quot;one of the few reasons to use the&lt;br&gt;
inverse&quot;.  If you're multiplying the inverse times a vector,&lt;br&gt;
then that's a clear signal that you should do something&lt;br&gt;
else.  Never ever multiply by the inverse.&lt;br&gt;
&lt;br&gt;
&amp;gt; Isn't it the case that if I use sum(Dij / f1(m1,m2) .*&lt;br&gt;
Dij, 2) for&lt;br&gt;
&amp;gt; each column that I would see a substantial slowdown in the&lt;br&gt;
calculation?&lt;br&gt;
&lt;br&gt;
Yes, that would be costly.  But it's not the right thing to do.&lt;br&gt;
&lt;br&gt;
&amp;gt; Is it worth it? Or is &quot;substantial re-use of the result of&lt;br&gt;
the logical&lt;br&gt;
&amp;gt; division&quot; one of the &quot;few reasons&quot; that is considered to&lt;br&gt;
justify&lt;br&gt;
&amp;gt; taking the inverse?&lt;br&gt;
&lt;br&gt;
No, this is a good reason to factor once and solve 1545&lt;br&gt;
times.  I'm going to rewrite your problem to shorten the&lt;br&gt;
expressions just a bit.  You want to compute s = sum ((b /&lt;br&gt;
A) .* b, 2) where A is used many times with different&lt;br&gt;
vectors b, right?&lt;br&gt;
&lt;br&gt;
For simplicity, and so I don't get it wrong, let me write&lt;br&gt;
this as backslash, and assume b is a column vector instead:&lt;br&gt;
&lt;br&gt;
s = sum (b'*(A\b)) ;&lt;br&gt;
&lt;br&gt;
Let's suppose A is unsymmetric and dense.  Then do this once:&lt;br&gt;
&lt;br&gt;
[L,U,p] = lu (A,'vector') ;&lt;br&gt;
lower.LT = true ;&lt;br&gt;
upper.UT = true ;&lt;br&gt;
&lt;br&gt;
and then do this many times with different b:&lt;br&gt;
&lt;br&gt;
x = linsolve (U, linsolve (L, b (p), lower), upper) ;&lt;br&gt;
s = sum (b'*x) ;&lt;br&gt;
&lt;br&gt;
or just a one-liner:&lt;br&gt;
&lt;br&gt;
x = sum (b' * insolve (U, linsolve (L, b(p), lower), upper));&lt;br&gt;
&lt;br&gt;
If that syntax looks ugly, try this (a little slower):&lt;br&gt;
&lt;br&gt;
[L,U,p]=lu(A,'vector') ;&lt;br&gt;
&lt;br&gt;
s = sum (b' * (U \ (L \ b (p)))) ;&lt;br&gt;
&lt;br&gt;
If that's still ugly, get LINFACTOR on the File Exchange and&lt;br&gt;
do this once:&lt;br&gt;
&lt;br&gt;
F = linfactor (A) ;&lt;br&gt;
&lt;br&gt;
and then do&lt;br&gt;
&lt;br&gt;
s = sum (b' * linfactor (A,b)) ;&lt;br&gt;
&lt;br&gt;
where linfactor.m will do the lu and linsolve's for you.  It&lt;br&gt;
will also do the more general thing (sparse case, and both&lt;br&gt;
LU and Chol).&lt;br&gt;
&lt;br&gt;
I wrote linfactor precisely to answer this very question.&lt;br&gt;
&lt;br&gt;
This will be more accurate and just as fast as:&lt;br&gt;
&lt;br&gt;
F = inv(A) ;&lt;br&gt;
&lt;br&gt;
s = sum (b'*(F*b)) ;&lt;br&gt;
&lt;br&gt;
which is what you're doing now.&lt;br&gt;
&lt;br&gt;
It's important to use the &quot;vector&quot; option for LU; otherwise,&lt;br&gt;
P*b takes a lot of time.&lt;br&gt;
&lt;br&gt;
Here are some timings, in MATLAB 7.4:&lt;br&gt;
&lt;br&gt;
&amp;gt;&amp;gt; A=rand(1545);&lt;br&gt;
&amp;gt;&amp;gt; b=rand(1545,1);&lt;br&gt;
&amp;gt;&amp;gt; tic; s = sum (b'*(A\b)); toc&lt;br&gt;
Elapsed time is 1.460618 seconds.&lt;br&gt;
&lt;br&gt;
&amp;gt;&amp;gt; tic; F=inv(A);toc&lt;br&gt;
Elapsed time is 2.420236 seconds.&lt;br&gt;
&amp;gt;&amp;gt; tic; s2 = sum (b'*(F*b)); toc&lt;br&gt;
Elapsed time is 0.006123 seconds.&lt;br&gt;
&lt;br&gt;
&amp;gt;&amp;gt; clear F&lt;br&gt;
&lt;br&gt;
&amp;gt;&amp;gt; tic ; F = linfactor(A) ; toc&lt;br&gt;
Elapsed time is 0.905961 seconds.&lt;br&gt;
&amp;gt;&amp;gt; F&lt;br&gt;
&lt;br&gt;
F = &lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;L: [1545x1545 double]&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;U: [1545x1545 double]&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;p: [1x1545 double]&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;kind: 'dense LU: L*U = A(p,:)'&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;code: 3&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&amp;gt;&amp;gt; tic; s = sum (b' * linfactor(F,b)) ; toc&lt;br&gt;
Elapsed time is 0.006120 seconds.&lt;br&gt;
&lt;br&gt;
So, since linfactor is the right thing to do in terms of&lt;br&gt;
accuracy, and since it's just as fast as F=inv(A) followed&lt;br&gt;
by F*b, why use inv?&lt;br&gt;
&lt;br&gt;
Note that linfactor doesn't emulate the b/A syntax (b/A is a&lt;br&gt;
tiny bit slower than backslash anyway; the slash operator in&lt;br&gt;
MATLAB is extremely simple; it just does (A'\b')' and uses&lt;br&gt;
backslash).</description>
    </item>
    <item>
      <pubDate>Wed, 03 Oct 2007 01:17:43 -0400</pubDate>
      <title>Re: Inverse of a Matrix</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/157000#394874</link>
      <author>Tim Davis</author>
      <description>&quot;Ravi &quot; &amp;lt;vioravis.nospam@gmail.com&amp;gt; wrote&lt;br&gt;
...&lt;br&gt;
&amp;gt; I am not in a position to avoid&lt;br&gt;
&amp;gt; using matrix inversion.&lt;br&gt;
&lt;br&gt;
Why?  What do you want to do with the inverse that you&lt;br&gt;
cannot avoid?  I'm very curious.</description>
    </item>
    <item>
      <pubDate>Wed, 03 Oct 2007 11:12:50 -0400</pubDate>
      <title>Re: Inverse of a Matrix</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/157000#394919</link>
      <author>Tim Davis</author>
      <description>&quot;Tim Davis&quot; &amp;lt;davis@cise.ufl.edu&amp;gt; wrote in message&lt;br&gt;
&amp;lt;fduqjn$bo3$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &quot;Ravi &quot; &amp;lt;vioravis.nospam@gmail.com&amp;gt; wrote&lt;br&gt;
&amp;gt; ...&lt;br&gt;
&amp;gt; &amp;gt; I am not in a position to avoid&lt;br&gt;
&amp;gt; &amp;gt; using matrix inversion.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Why?  What do you want to do with the inverse that you&lt;br&gt;
&amp;gt; cannot avoid?  I'm very curious.&lt;br&gt;
&lt;br&gt;
&amp;lt;humor&amp;gt;&lt;br&gt;
Are you needing to use the pseudo-inverse applied to delta&lt;br&gt;
functions?  I anxiously await your reply;&lt;br&gt;
I'm on pinv and needles. ;-)&lt;br&gt;
&amp;lt;/humor&amp;gt;</description>
    </item>
    <item>
      <pubDate>Wed, 19 Aug 2009 10:55:05 -0400</pubDate>
      <title>Re: Inverse of a Matrix</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/157000#674194</link>
      <author>Vanesa </author>
      <description>&quot;Tim Davis&quot; &amp;lt;davis@cise.ufl.edu&amp;gt; wrote in message &amp;lt;fdup2q$j8o$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in &lt;br&gt;
&amp;gt; ...&lt;br&gt;
&amp;gt; &amp;gt; I am working with a function &lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; Pij = g( Dij * inv(f1(m1,m2)) * Dij' + log(det(f1(m1,m2))),&lt;br&gt;
&amp;gt; &amp;gt;          Dij * inv(f2(m1,m2)) * Dij' + log(det(f2(m1,m2))) )&lt;br&gt;
&amp;gt; &amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; Where&lt;br&gt;
&amp;gt; &amp;gt;  Dij is ([m1;m2](i,:) - [m1;m2](j,:)) -- i.e., a&lt;br&gt;
&amp;gt; difference vector&lt;br&gt;
&amp;gt; &amp;gt;  f1(m1,m2) is a matrix function in the covariances of m1&lt;br&gt;
&amp;gt; and m2&lt;br&gt;
&amp;gt; &amp;gt;  f2(m1,m2) is a matrix function closely related to f1&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; What you want to do is factorize the matrix A = f1(m1,m2)&lt;br&gt;
&amp;gt; once and then reuse the factorization lots of times.  This&lt;br&gt;
&amp;gt; isn't the case of &quot;one of the few reasons to use the&lt;br&gt;
&amp;gt; inverse&quot;.  If you're multiplying the inverse times a vector,&lt;br&gt;
&amp;gt; then that's a clear signal that you should do something&lt;br&gt;
&amp;gt; else.  Never ever multiply by the inverse.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; Isn't it the case that if I use sum(Dij / f1(m1,m2) .*&lt;br&gt;
&amp;gt; Dij, 2) for&lt;br&gt;
&amp;gt; &amp;gt; each column that I would see a substantial slowdown in the&lt;br&gt;
&amp;gt; calculation?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Yes, that would be costly.  But it's not the right thing to do.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; Is it worth it? Or is &quot;substantial re-use of the result of&lt;br&gt;
&amp;gt; the logical&lt;br&gt;
&amp;gt; &amp;gt; division&quot; one of the &quot;few reasons&quot; that is considered to&lt;br&gt;
&amp;gt; justify&lt;br&gt;
&amp;gt; &amp;gt; taking the inverse?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; No, this is a good reason to factor once and solve 1545&lt;br&gt;
&amp;gt; times.  I'm going to rewrite your problem to shorten the&lt;br&gt;
&amp;gt; expressions just a bit.  You want to compute s = sum ((b /&lt;br&gt;
&amp;gt; A) .* b, 2) where A is used many times with different&lt;br&gt;
&amp;gt; vectors b, right?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; For simplicity, and so I don't get it wrong, let me write&lt;br&gt;
&amp;gt; this as backslash, and assume b is a column vector instead:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; s = sum (b'*(A\b)) ;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Let's suppose A is unsymmetric and dense.  Then do this once:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; [L,U,p] = lu (A,'vector') ;&lt;br&gt;
&amp;gt; lower.LT = true ;&lt;br&gt;
&amp;gt; upper.UT = true ;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; and then do this many times with different b:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; x = linsolve (U, linsolve (L, b (p), lower), upper) ;&lt;br&gt;
&amp;gt; s = sum (b'*x) ;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; or just a one-liner:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; x = sum (b' * insolve (U, linsolve (L, b(p), lower), upper));&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; If that syntax looks ugly, try this (a little slower):&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; [L,U,p]=lu(A,'vector') ;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; s = sum (b' * (U \ (L \ b (p)))) ;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; If that's still ugly, get LINFACTOR on the File Exchange and&lt;br&gt;
&amp;gt; do this once:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; F = linfactor (A) ;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; and then do&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; s = sum (b' * linfactor (A,b)) ;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; where linfactor.m will do the lu and linsolve's for you.  It&lt;br&gt;
&amp;gt; will also do the more general thing (sparse case, and both&lt;br&gt;
&amp;gt; LU and Chol).&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I wrote linfactor precisely to answer this very question.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; This will be more accurate and just as fast as:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; F = inv(A) ;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; s = sum (b'*(F*b)) ;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; which is what you're doing now.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; It's important to use the &quot;vector&quot; option for LU; otherwise,&lt;br&gt;
&amp;gt; P*b takes a lot of time.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Here are some timings, in MATLAB 7.4:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; A=rand(1545);&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; b=rand(1545,1);&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; tic; s = sum (b'*(A\b)); toc&lt;br&gt;
&amp;gt; Elapsed time is 1.460618 seconds.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; tic; F=inv(A);toc&lt;br&gt;
&amp;gt; Elapsed time is 2.420236 seconds.&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; tic; s2 = sum (b'*(F*b)); toc&lt;br&gt;
&amp;gt; Elapsed time is 0.006123 seconds.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; clear F&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; tic ; F = linfactor(A) ; toc&lt;br&gt;
&amp;gt; Elapsed time is 0.905961 seconds.&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; F&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; F = &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;        L: [1545x1545 double]&lt;br&gt;
&amp;gt;        U: [1545x1545 double]&lt;br&gt;
&amp;gt;        p: [1x1545 double]&lt;br&gt;
&amp;gt;     kind: 'dense LU: L*U = A(p,:)'&lt;br&gt;
&amp;gt;     code: 3&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; tic; s = sum (b' * linfactor(F,b)) ; toc&lt;br&gt;
&amp;gt; Elapsed time is 0.006120 seconds.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; So, since linfactor is the right thing to do in terms of&lt;br&gt;
&amp;gt; accuracy, and since it's just as fast as F=inv(A) followed&lt;br&gt;
&amp;gt; by F*b, why use inv?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Note that linfactor doesn't emulate the b/A syntax (b/A is a&lt;br&gt;
&amp;gt; tiny bit slower than backslash anyway; the slash operator in&lt;br&gt;
&amp;gt; MATLAB is extremely simple; it just does (A'\b')' and uses&lt;br&gt;
&amp;gt; backslash).&lt;br&gt;
Hi&lt;br&gt;
&lt;br&gt;
I guess i have a similar problem...I'm trying to find the positive part of a matrix, lets call it A+ of A (I dont know if that is the correct name) but is simply the matrix with ONLY the positive eigenvalues (diagonal matrix) of A multiplied by the right eigenvector matrix and the inverse of the right eigenvector matrix.&lt;br&gt;
&lt;br&gt;
I dont know if my approach is correct but what I tried to do  was to simply multiply teh positive eigenvalue matrix by the right eigenvector matrix. Then I applied linsolve, i get an upper triangular matrix but the lower matrix is simply a diagonal matrix of ones. and so the system has no solution. (I get many NaN's and the matrix really does not mmake much sense to me)&lt;br&gt;
&lt;br&gt;
I applied linfactor and get exactly the same result.&lt;br&gt;
&lt;br&gt;
Does someone have any idea how to get A+?&lt;br&gt;
&lt;br&gt;
I'd appreciate your help very much.&lt;br&gt;
&lt;br&gt;
Thank you.</description>
    </item>
  </channel>
</rss>

