title: Calculating an inverse matrix in Matlab
body: <p>I'm running an optimization algorithm that requires calculation of the inverse of a matrix. The goal of the algorithm is to eliminate negative values from the matrix A and obtain the new matrix B. Basically, I start with known square matrices B and C of the same size.</p>
  
  <p>I start by calculating the matrix A which is equal to:</p>
  
  <blockquote>
    <p>A = B^-1 * C</p>
  </blockquote>
  
  <p>Or in Matlab:</p>
  
  <blockquote>
  <pre><code>A = B\C;
  </code></pre>
  </blockquote>
  
  <p>I use this because Matlab told me <code>B\C</code> is more accurate than <code>inv(B)*C</code>.</p>
  
  <p>The negative values in A are then divided by two and A is then normalised so that it's rows have length of 1. Using this new A, I calculate a new B with:</p>
  
  <blockquote>
    <p>(1/N) * A * C' = B^-1</p>
  </blockquote>
  
  <p>where N is just a scaling factor (# of columns in A). This new B would then be used again in the first step and these iterations continue until the negatives in A are gone.</p>
  
  <p>My problem is I have to calculate B from the second equation and then normalise it.</p>
  
  <blockquote>
  <pre><code>invB = (1/N)*A*C';
  B = inv(invB);
  </code></pre>
  </blockquote>
  
  <p>I've been calculating B using <code>inv(B^-1)</code> but after a few iterations I start getting messages that <code>B^-1</code> is "close to singular or badly scaled." </p>
  
  <p>This algorithm actually works for smaller matrices (around 70x70) but when it gets up to about 500x500 I start getting these messages.</p>
  
  <p>Are there any better ways to calculate <code>inv(B^-1)</code>?</p>