This is what I am trying to do,
simulation <- function(n_trucks,capacity,tons,hours,PI,PO){

Truck Capacity application

   c_truck <- capacity[1]+congruencial(n_trucks)*(capacity[2]-capacity[1]) 
   n_camiones<-n_trucks[1]

Trash of the Block

t_trash <- tons[2]*qnorm(congruencial(1))+tons[1]

Travel numbers

trashinarea<-matrix(rnorm(n = 5*5,mean = 10,sd = 5),nrow = 5,ncol = 5)
trashinarea[1,1]<-0
inital_position<-trashinarea[PI[1]]
out_position<-trashinarea[PO[1]]
t_recolection <- 0
t_transit <- 0
t_finish_backzoneA <- 0
while (t_trash >0 && t_trash <= c_truck && x==t_finish_backzoneA) {
for (i in 1:trashinarea) {
if(n_camiones==t_recolection)
inital_position<-t_recolection
if(X==t_recolection){
out_position[n_camiones]<-t_transit

  }
    t_recolection[i] <- 0.5*qnorm(congruencial(1))+1
  t_transit[i] <- 0.5*qnorm(congruencial(1))+1
  t_finish_backzoneA[i] <- 0.5*qnorm(congruencial(1))+1  
  x <- cumsum(c(t_recolection,t_transit,t_finish_backzoneA))
  if (x[i*3]>=hours)break
  
}

I have some problems imagining how to tell R this truck will go to the initial position [1,1] because I have 4 trucks and all of them goes and come back but each truck will be in and out of the position as an example: a truck on e[1,1], truck 2=[3,5], etc, when they return and they go out they will use the same position to go out . . My question is, do I need to create a different matrix to make it possible to know or matrix 1= truck goes here, for matrix 2= 2 truck goes here. I need some advice on this,