Q[j-2] gives you Q[0] in your first inner loop iteration. R arrays start at one. B. > On 2018-06-13, at 07:21, Maija Sirkjärvi <maija.sirkjarvi using gmail.com> wrote: > > Amat[J-1+j-2,j-1]= 1/(Q[j] - Q[j-1]) + 1/(Q[j-1] - Q[j-2])