Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有对称矩阵(例如M <- matrix(1:4, ncol = 2)),我想将非对角线元素设置为0。还有比这更聪明的方法diag(diag(M), ncol = ncol(M))吗?
M <- matrix(1:4, ncol = 2)
0
diag(diag(M), ncol = ncol(M))
注意:diag不支持使用diag(M, ncol = ncol(M)),也没有类似diag(M, as.matrix = TRUE)etc的选项。
diag
diag(M, ncol = ncol(M))
diag(M, as.matrix = TRUE)