title: Terms in loop contains their sum
body: <p>I would like to write a code that generates <code>3 x 1</code> vector <code>y</code> according to following rule (The small numbers are selected for simplicity):</p>
  
  <p><a href="https://i.stack.imgur.com/2xKuE.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/2xKuE.png" alt="enter image description here"></a></p>
  
  <p>Here <code>x</code> is a <code>3 x 1</code> vector. According to the rule, for an update of <code>y</code>, I need sum of all <code>y</code>’s. </p>
  
  <p>An attemp to code with an arbitrary <code>x</code>:</p>
  
  <pre><code>x &lt;- c(2,3,1)
  y &lt;- c(0,0,0)
  
  for(i in 1:5){
    for(j in 1:3){
      y[j] &lt;- x[j] + y[j] + sum(y)
    }
  }
  </code></pre>
  
  <p>This code is not appropriate because it computes <code>sum(b)</code> term by term. 
  The inner loop indicates something like this:</p>
  
  <pre><code>y[1] = x[1] + 0 = 2
  y[2] = x[2] + 2 = 5
  y[3] = x[3] + 2 + 5 = 8
  </code></pre>
  
  <p>It is not appropriate because <code>sum(y)</code> term contains one term for <code>y[1]</code>, two terms for <code>y[2]</code>, three terms for <code>y[3]</code>. But I think <code>sum(y)</code> should be <code>2 + 5 + 8 = 15</code> for each iteration, <code>y[1], y[2], y[3]</code>,  according to the rule given above. Moreover this procedure should be repeated for a certain times (here 5 times shown by the outer loop). At each time of outer loop, only one <code>sum(y)</code> term will be computed for all three iteration of inner loop and it will be put as <code>sum(y)</code> term for each <code>j</code>.</p>
  
  <p>How should I code this?</p>