Random matrix with diagonal entries 0's and all other entries are 0's and 1's

122 Views Asked by At

I tried using the rbern function in R but I realized that the diagonal entries are not all 0's.

1

There are 1 best solutions below

0
On

This would be a possible way:

m <- 10
n <- 10
mat <- matrix(sample(0:1,m*n, replace=TRUE),m,n)
diag(mat) <- 0

#> mat
#      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
# [1,]    0    1    1    0    1    0    0    0    1     0
# [2,]    1    0    1    1    0    0    1    0    0     1
# [3,]    0    0    0    1    0    1    0    0    1     0
# [4,]    0    1    0    0    0    0    0    0    1     0
# [5,]    0    1    1    1    0    0    1    1    0     0
# [6,]    1    0    1    0    1    0    0    0    1     0
# [7,]    1    1    1    0    0    0    0    0    1     1
# [8,]    1    0    1    1    1    1    1    0    1     1
# [9,]    1    1    1    1    1    1    0    0    0     1
#[10,]    1    0    1    0    1    0    0    0    1     0