具有对角项0且所有其他项均为0和1的随机矩阵

问题描述 投票:0回答:1

我尝试在R中使用rbern函数,但我意识到对角线条目并非全为0。

r probability statistics-bootstrap bernoulli-probability bernoulli-numbers
1个回答
0
投票

这可能是一种方法:

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

0
投票

我们也可以尝试

mat[lower.tri(mat) == upper.tri(mat)] <- 0

数据

m <- 10
n <- 10
mat <- matrix(sample(0:1,m*n, replace=TRUE),m,n)
© www.soinside.com 2019 - 2024. All rights reserved.