title: Getting outputs from a "for" loop in r into a matrix or data frame
body: <p>I am trying to perform the following loop:</p>
  <ol>
  <li><p>Compare each of the n rows of my matrix X with a X.prototype matrix.</p>
  </li>
  <li><p>Find the closest row of X.prototype to each row of X.</p>
  </li>
  <li><p>if the two rows have the same label (y and y.prototype), then bring the values close; otherwise, put them further.</p>
  </li>
  </ol>
  <p>My code:</p>
  <pre><code>for (i in (1:n)){ 
    
    closest.row &lt;- which.min(colSums((t(X.prototype) - X[i,])^2))
    
    X.new.x1 &lt;- ifelse(y.prototype[closest.row] == y[i],
                       X.prototype[closest.row,1]+(eta*(X[i,1]-X.prototype[closest.row,1])),
                       X.prototype[closest.row,1]-(eta*(X[i,1]-X.prototype[closest.row,1])))
    X.new.x2&lt;- ifelse(y.prototype[closest.row] == y[i],
                      X.prototype[closest.row,2]+(eta*(X[i,2]-X.prototype[closest.row,2])),
                      X.prototype[closest.row,2]-(eta*(X[i,2]-X.prototype[closest.row,2])))
    X.new &lt;- matrix(c(X.new.x1,X.new.x2),ncol=2) 
  
    plot(X.new.x2~X.new.x1)
  
   print(X.new)
    
  }
  
  
  ----------
  
  
  set.seed(123)                        # Set seed for reproducibility
  n &lt;- 100
  X &lt;- cbind(x1 = runif(n, -1.5, 1.5),
             x2 = runif(n, -1.5, 1.5)) # Generate random points
  y &lt;- as.integer(rowSums(X^2)&lt;1)      # Determine whether inside the circle#
  idx &lt;- sample(100, 10)               # Mess up 10 class labels ...
  y[idx] &lt;- 1-y[idx]                   # ... by flipping the label
  
  is &lt;- c(sample(which(y==0),K), sample(which(y==1),K))
  X.prototype &lt;- X[is,]
  y.prototype &lt;- y[is]                 # Will be K times 0, followed by K times 1
  
  K &lt;- 10
  eta &lt;- 0.25
  H &lt;- 25
  </code></pre>
  <p>The problem is that the outcomes come out as single vectors, and I am unable to bring them together in a <code>X.new</code> matrix, to use for new predictions and to plot it.</p>
  <p><a href="https://i.stack.imgur.com/TWY9V.png" rel="nofollow noreferrer">this is a link to how my output looks</a></p>
  <p><a href="https://i.stack.imgur.com/vVhER.png" rel="nofollow noreferrer">This is how I'd like them to look</a></p>
  <p>Thank you
  S</p>