Search code examples
rmatrix-indexingsubmatrix

Matrix without a particular submatrix in R


I have randomly generated a submatrix “train” from “mat” using the following:

train <- mat[sample(nrow(mat), size = 317, replace = FALSE),] 

My question is, how do I then create “test” as a submatrix of “mat” which excludes the matrix “train”?

Thanks!


Solution

  • train.index are the indexes used for training.

    mat <- matrix(rnorm(20000),nrow=1000)
    train.index <-sample(nrow(mat), size = 317, replace = FALSE)
    train <- mat[train.index,]
    test <- mat[-train.index,]
    

    Try it online!