title: coding double sum in R
body: <p>I would like to compute these to  quantities </p>
  
  <pre><code>a12=sum_(i from 1 to m)sum_(j1&lt;j2)(I(X[i]&gt;Y[j1] and X[i]&gt;Y[j2]))
  
  a13=sum_(j from 1 to n)sum_(i1&lt;i2)(I(X[i1]&gt;Y[j] and X[i2]&gt;Y[j]))
  </code></pre>
  
  <p>where I is the indicator function.</p>
  
  <p>So I came up with this R code</p>
  
  <pre><code>a12=0; a13=0
  
  for (l in 1:(length(Z1)-1)){
  
   for (m in  1:(length(Z2)-1)){
  
   a12&lt;-a12+(Z1[l]&lt;Z2[m])*(Z1[l+1]&lt;Z2[m])*1
  
   a13&lt;-a13+(Z1[l]&lt;Z2[m])*(Z1[l]&lt;Z2[m+1])*1
  
          } # closing m
  
            } # closing l
  
      a12=a12+sum((Z1[-length(Z1)]&lt;Z2[length(Z2)])*(Z1[-1]&lt;Z2[length(Z2)])*1)
  
      a13=a13+sum((Z1[length(Z1)]&lt;Z2[-length(Z2)])*(Z1[length(Z1)]&lt;Z2[-1])*1)
  
  
  a12;
  a13
  </code></pre>
  
  <p>Unfortunately, not only this is very slow but I am not getting what I am supposed to get.</p>
  
  <p>Could you help me, please!</p>
  
  <p>Thanks,</p>
  
  <p>Roland</p>