vendredi 25 septembre 2020

Adding adjustable random noise to a matrix in R

I have a matrix generating function that produces lower-triangle of 1s and upper-triangle of 0s.

I was wondering if it might be possible to add some adjustable random noise (from some distribution that gives random 0 and 1) to the outputted matrix such that the random 0s randomly replace some of the bottom 1s, and random 1s randomly replace some of the top 0s?

lower_mat <- function(r, c) {
  m <- matrix(0, nrow=r,ncol=c)
  m[lower.tri(m)] <- 1
  m
}


lower_mat(5,4)
#      [,1] [,2] [,3] [,4]
# [1,]    0    0    0    0
# [2,]    1    0    0    0
# [3,]    1    1    0    0
# [4,]    1    1    1    0
# [5,]    1    1    1    1



Aucun commentaire:

Enregistrer un commentaire