How to insert data in a matrix with a for loop in specific positions of the matrix. for example the position [4,4]=0; [5.20]=0 [17.3]=0, and at the same time place random numbers from a normal distribution. is that possible? any other method?
Just simply use matrix indexing, for instance, if you have matrix A with 5 rows and 10 columns you can find the cell values using the index [i, j]. That's mean, if you want the value at position 3rd row and 6th column of a matrix A then you have to call A[3, 6]. So, you can define a random matrix from a normal distribution :
A <- matrix(rnorm(5*10), nrow = 5)
# assign '0' values at 3rd rows and 6th column position
A[3, 6] <- 0
If you have several positions to change you can simply add one by one
A[4, 2] <- 0
A[5, 6] <- 0
....
If you have many changes and you want to use loop:
for (i in row_index) {
for (j in col_index) {
for(k in values) {
A[row_index[i], col_index[j]] <- values[k]
}
}
}
You must define row_index , column_index and values before executing the loop.
}
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,
Hi,
I am not clear quite clear what you exactly want. From my current understanding, you might define transport position using a matrix. If so, then you can use a 3D array. Since you have 4 trucks, whatever size, you can define an array:
The in-out position will be different for each of the trucks. All can go from the initial position [1, 1] to any other position. Hope this will help...