title: How to calculate this in matlab without for loop
body: <p>Since matlab is slow when executing for loop, I usually avoid for loop for all my codes, and turn those into matrix calculation, which would be way fast. But here is a problem I can not find a smart way:</p>
  
  <p>I have a n x n matrix</p>
  
  <pre><code>A=[a1,a2,a3,...,an], 
  </code></pre>
  
  <p>here a1,a2,a3....an are columns of the matrix.</p>
  
  <p>Another n x n matrix </p>
  
  <pre><code>B=[b1,b2,b3,...,bn], 
  </code></pre>
  
  <p>similarly b1,b2,b3... are also columns of B.</p>
  
  <p>And also a n x n matrix M.</p>
  
  <p>I want to calculate the n x n matrix </p>
  
  <pre><code>C=[c1,c2,c3,...,cn], 
  
  thus (M+diag(ai))*ci = bi.
  </code></pre>
  
  <p>namely </p>
  
  <pre><code>ci = (M+diag(ai))\bi.
  </code></pre>
  
  <p>I know one way without for loop is:</p>
  
  <pre><code>C(:)=( blkdiag(M)+diag(A(:)) )\B(:).
  </code></pre>
  
  <p>But this will do too much calculation than needed.</p>
  
  <p>Any smart solutions? you can assume there is no singularity problem in the calculation.</p>