matrix question

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?

Thank you

Here is for a one-dimensional vector. It is not too different for an n-dimensional matrix:

x <- rnorm(n=50, mean=100, sd=15)
x[4] <- 0
x[5] <- 0
x[17] <- 0
x

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.

thank you, I will try this .

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,

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:

truck_trans <- array(
  NA,  
  dim = c(5, 5, 4),  
  dimnames = list(paste0("In", 1:5), paste0("Out", 1:5), paste0("Truck", 1:4))
  )
truck_trans

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...

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.