-1

I am trying to create a matrix of coordinates(indexes) that I randomly pick one from using the sample function. I then use these to select a cell in another matrix. What is the best way to do this? The trouble is how to store these integers in the matrix so that they are easy to separate. Right now I have them stored as strings with a comma, that I then split. Someone suggested I use a pair, or a string, but I cannot seam to get these to work with a matrix. Thanks!

EDIT:What i currently have looks like this (changed a little to make sense out of context):


probs <- matrix(c(0,0,0.6,0,0,
                  0,0.7,1,0.7,0,
                  0.6,1,0,1,0.6,
                  0,0.7,1,0.7,0,
                  0,0,0.6,0,0),5,5)
  
cordsMat <- matrix("",5,5)

for (x in 1:5){
  for (y in 1:5){
      
    cordsMat[x,y] = paste(x,y,sep=",")
      
  }
}



cords <- sample(cordsMat,1,,probs)
cordsVec <- unlist(strsplit(cords,split = ","))
cordX <- as.numeric(cordsVec[1])
cordY <- as.numeric(cordsVec[2])

otherMat[cordX,cordY]
            

It sort of works but i would also be interested for a better way, as this will get repeated a lot.

Fegrus
  • 3
  • 2
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Nov 25 '20 at 01:12
  • let the `mat` matrix be composed of the coordinates i.e two columns and n rows, simply run `rand <- sample(1:nrow(mat), 1)` => `other.mat[mat[rand,1], mat[rand,2]]` – Abdessabour Mtk Nov 25 '20 at 01:34
  • 2
    What you're calling a `matrix` in R is really a 2x2 `array`. In an array (2D or otherwise), all elements must be atomic (single int, single dbl, single string, etc). **But**, if all of your "tuples" are (e.g.) length 2, then you can turn your 2D matrix (dim `MxN`) of tuples into a 3D array (dim `MxNx2`). – r2evans Nov 25 '20 at 01:35
  • @MrFlick, Sorry i have added an example – Fegrus Nov 25 '20 at 03:19
  • @AbdessabourMtk I'm not sure that would work with the weighted probability i am using. Sorry, i should have put the example in to start with – Fegrus Nov 25 '20 at 03:20
  • @r2evans I think this is the way to go. This definately makes the most sense for storing the coordinates, but would you mind explaining how this will work with sample? I have had a play but it only returns single integers, where i think i would need whole entries. Thanks! – Fegrus Nov 25 '20 at 03:37
  • @r2evans also, will this work with the probability weights in a matrix as i currently have them in the example, sorry i was late adding this – Fegrus Nov 25 '20 at 03:42
  • Is there a reason that you are using a matrix for `cordsMat`? E.g., `cordsLst <- lapply(as.list(c(cordsMat)), function(a) as.integer(strsplit(a, ",")[[1]]))` to convert from your current matrix to a list of integer pairs (not too distant from "tuples"), I recommend generating this directly instead of via a matrix; from there, `sample(cordsLst, 1)[[1]]` will get you a pair of indices. If you create a vector of probabilities/weights, it would be `sample(cordsLst, 1, probs=somevec)[[1]]`. – r2evans Nov 25 '20 at 11:29
  • @Fegrus check my edited answer, using expand.grid automatically generates the coordinates vector no need to paste it, we sample a random row number and use it to get the random value – Abdessabour Mtk Nov 25 '20 at 20:25

1 Answers1

0

If you want to set the probabilities it can easily be done by providing it to sample

# creating the matrix
matrix(sample(rep(1:6, 15:20), 25), 5) -> other.mat
# set the probs vec
probs <-  c(0,0,0.6,0,0,
           0,0.7,1,0.7,0,
           0.6,1,0,1,0.6,
           0,0.7,1,0.7,0,
           0,0,0.6,0,0)
# the coordinates matrix
mat <- as.matrix(expand.grid(1:nrow(other.mat),1:ncol(other.mat)))
# sampling a row randomly
sample(mat, 1, prob=probs) -> rand
# getting the value
other.mat[mat[rand,1], mat[rand,2]]
[1] 6
Abdessabour Mtk
  • 3,895
  • 2
  • 14
  • 21