title: 26. How do I compute a partial integral across a 2D matrix in Matlab?
body: <p>I have a 2D matrix, Z(x,y) from which I need to compute the equation:</p>
  
  <p><a href="https://i.stack.imgur.com/K14jT.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/K14jT.png" alt="enter image description here"></a></p>
  
  <p>I do this using the code:</p>
  
  <pre><code>Sa        = nansum(nansum(abs(Z - Zbar))) / (nX*nY);
  </code></pre>
  
  <p>N/B: I am using <code>nansum</code> because <code>Z(x,y)</code> contains some <code>NaN</code> values.</p>
  
  <p>Now, I’m finding it difficult to compute the equation:</p>
  
  <p><a href="https://i.stack.imgur.com/IuoaG.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/IuoaG.png" alt="enter image description here"></a></p>
  
  <p>I tried to break the equation down with the following code: </p>
  
  <pre><code>% compute the partial sums in the equation
  for jj = 1: nY-1
      for ii = 1: nX-1
          Z_abt(jj,ii) = ( Z(jj,ii)- Z(jj,ii+1) )^2 + ( Z(jj,ii)- Z(jj+1,ii) )^2;       
      end
  end
  
  % compute Sdr
  Sdr = nansum( nansum( sqrt(1+Z_abt) - 1) );
  </code></pre>
  
  <p><code>Sdr</code> values are always supposed to be fractions, but I am getting ridiculously large values Instead. </p>
  
  <p>I’m just wondering if I really need a loop to do this and how I could go about that?</p>