title: How to fix "Subscript out of bounds error"
body: <p>I have a R code applied on certain data X and Y, The code is rather simple but it gives certain error </p>
  
  <pre><code>"Error in `[&lt;-`(`*tmp*`, R, , value = c(1, 1)) : subscript out of bounds"
  </code></pre>
  
  <p>Can anyone please help me to sort it out.</p>
  
  <pre><code>#X &lt;- read.table("data1.dat")
  #Y &lt;-read.table("data2.dat")
  
  X &lt;- c(1,2,43,1,6,5,44,6.8,7.8,9.8)
  Y &lt;- c(1,3,31,26,78,4,5,44,4,6)
  
  length(X)
  length(Y)
  
  Rmax &lt;- 15
  data_len &lt;- length(X)
  Z &lt;- as.matrix(cbind(X,Y))
  Cen &lt;- matrix(rnorm(2*Rmax),Rmax,2)
  Xe &lt;- as.matrix(cbind(1,X))
  w &lt;-  matrix(rnorm(2*Rmax),Rmax,2)
  lambda &lt;- matrix(rnorm(1*Rmax),Rmax,1)
  U &lt;- matrix(rnorm(1*Rmax),Rmax,1)
  gamma &lt;- matrix(rnorm(2*Rmax),Rmax,2)
  P_matrix &lt;- 10^3*matrix(rnorm(2*2),2,2)
  Yhat &lt;- matrix(,data_len,1)
  a1 &lt;- 0
  a2 &lt;- 0
  a4 &lt;- matrix(rnorm(2),1,2)
  a3 &lt;- 0
  Pz &lt;- 0
  P &lt;- matrix(rnorm(1*Rmax),Rmax,1)
  dist &lt;- numeric(Rmax)
  r &lt;- 0.04
  m3 &lt;- 0
  k &lt;- 1
  
  while (k &lt; length(X)){  
   if(k == 1){  
   Cen[1,] &lt;- Z[k,]; U[k,] &lt;- 1; lambda &lt;- matrix(sapply(U,function(x){x/sum
  
  (U)}),Rmax,1); Yhat[k,] &lt;- Xe[k,]%*%(t(w)%*%lambda)
  
  } else {
  
  a1 &lt;- big_V(Z[k,]);a2 &lt;- a2+ big_V(Z[k-1,]);a4 &lt;- a4 + Z[k-1,];a3 &lt;- sum(Z[k,]*a4)
  
  B &lt;- ((k-1)*(a1+1))+a2-(2*a3); Pz &lt;- (k-1)/B
  
  for(i in 1:length(P)){
      P[i] &lt;- ((k-1)*P[i])/((k-2)+(P[i])+(P[i])*(dist(Z[k-1,],Z[k,])))
  }
  print(k)
  for (i in 1:Rmax){
   dist[i] &lt;- abs(sqrt(sum((Z[k,]-Cen[i,])^2)))
  }
  
  p1 &lt;- min(dist)
  p2 &lt;- which.min(dist)
  
  m1 &lt;- max(P)
  m2 &lt;- which.max(P)
  m3 &lt;- (p1/r)+(Pz/m1)
  
  if (Pz &gt; m1 &amp; m3 &lt; 1){
  Cen[m2,] &lt;- Z[k,]
  P[m2] &lt;- Pz
  }
  
  
  for (i in 1:Rmax){
  U[i] &lt;- exp(-r*(abs(sqrt(sum((X[k,]-Cen[i,1])^2)))))
  }
  
  lambda &lt;- sapply(U, function(x) x/sum(U)) 
  
  P_matrix &lt;- (diag(2)-(t(gamma)%*%lambda%*%Xe[k,]))%*%P_matrix
  
  A &lt;- lambda%*%Xe[k,]
  Z1 &lt;- P_matrix%*%t(A)
  Z2 &lt;- A%*%P_matrix%*%t(A)
  Z3 &lt;- diag(15)+Z2
  gamma &lt;- t(Z1%*%solve(Z3))
  
  
  w_bar &lt;- Xe[1,]%*%t(w)%*%lambda
  w &lt;-  w + (gamma*diag(e))
  
  }  
  } 
  Yhat &lt;- Xe%*%t(w)%*%lambda
  </code></pre>
  
  <p>I have shortened the data for quick calculation.</p>