I a have symmetric matrix (e.g. M <- matrix(1:4, ncol = 2)
) and I want to set the off diagonal elements to 0
. Is there a smarter way than diag(diag(M), ncol = ncol(M))
?
Note: diag
does not support the usssage of diag(M, ncol = ncol(M))
and doesn't have an option like diag(M, as.matrix = TRUE)
etc.