title: Matlab to R For Loop Conversion
body: <p>I'm currently trying to convert MATLAB code to R and run a loop to find the derivative of an equation.</p>
  
  <p>MATLAB:</p>
  
  <pre><code>for i=1:length(x)
      dx(i,:) = lorenz(0,x(i,:),sigma,beta,rho);
  end
  </code></pre>
  
  <p>My attempted code in R:</p>
  
  <pre><code>
  parameters &lt;- c(s = 10, r = 28, b = 8/3)
  state &lt;- c(X = -8, Y = 7, Z = 27)
  
  lorenz &lt;- function(t, state, parameters) {
      with(as.list(c(state, parameters)), {
          dX &lt;- s * (Y - X)
          dY &lt;- X * (r - Z) - Y
          dZ &lt;- X * Y - b * Z
          list(c(dX, dY, dZ))
      })
  }
  
  times &lt;- seq(0.001, 1, by = 0.001)
  
  out &lt;- ode(y = state, times = times, func = lorenz, parms = parameters)
  
  dx &lt;- NULL
  for (i in 1:1000){
      dx[i,] &lt;- lorenz(0,state[i,],parameters)
  }
  </code></pre>
  
  <p>I keep receiving the error "Error in state[i, ] : incorrect number of dimensions", any idea why?</p>