title: Compute double sum in matlab efficiently?
body: <p>I am looking for an optimal way to program this summation ratio. As input I have two vectors <code>v_mn</code> and <code>x_mn</code> with <code>(M*N)x1</code> elements each.</p>
  
  <p>The ratio is of the form:    </p>
  
  <p><img src="https://latex.codecogs.com/gif.latex?r%3D%5Cdfrac%7Bv_%7Bmn%7Dx_%7Bmn%7D%7D%7B1&plus;%5Csum%5Climits_%7Bm%27%3D1%2Cm%27%5Cneq%20m%7D%5E%7BM%7D%5Csum%5Climits_%7Bn%27%3D1%2Cn%27%5Cneq%20n%7D%5E%7BN%7Dv_%7Bmn%27%7Dx_%7Bm%27n%27%7D%7D" alt="formula"></p>
  
  <p>The vector <code>x_mn</code> is <code>0-1</code> vector so when <code>x_mn=1</code>, the ration is <code>r</code> given above and when <code>x_mn=0</code> the ratio is <code>0</code>.</p>
  
  <p>The vector <code>v_mn</code> is a vector which contain real numbers.</p>
  
  <p>I did the denominator like this but it takes a lot of times.</p>
  
  <pre><code>function r_ij = denominator(v_mn, M, N, i, j)
  %here x_ij=1, to get r_ij.
  S = [];
  for m = 1:M
    for n = 1:N
      if (m ~= i)
        if (n ~= j)
          S = [S v_mn(i, n)];
        else
          S = [S 0];
        end
      else
        S = [S 0];
      end
    end
  end
  r_ij = 1+S;
  end
  </code></pre>
  
  <p>Can you give a good way to do it in matlab. You can ignore the ratio and give me the denominator which is more complicated.</p>
  
  <p>EDIT: I am sorry I did not write it very good. The <code>i</code> and <code>j</code> are some numbers between <code>1..M</code> and <code>1..N</code> respectively. As you can see, the ratio <code>r</code> is many values (<code>M*N</code> values). So I calculated only the value <code>i</code> and <code>j</code>. More precisely, I supposed <code>x_ij=1</code>. Also, I convert the vectors <code>v_mn</code> into a matrix that's why I use double index.</p>