Reassign values in the non diagonal part of a matrix similar use as its diag(x) and diag(x) <- value equivalent

non.diagonal(x) <- value

Arguments

x

matrix or other R object with length(dim(x)) == 2

value

scalar or vector of values to replace the non diagonal part of x with. Filled in by increasing row then by increasing column,

Value

a matrix with similar dimensions as x where the non-diagonal parts have been replaced by the value in value

Examples

set.seed(42) M <- matrix(sample(1:10,16,replace = TRUE),4,4) M
#> [,1] [,2] [,3] [,4] #> [1,] 1 10 1 9 #> [2,] 5 4 8 5 #> [3,] 1 2 7 4 #> [4,] 9 10 4 10
non.diagonal(M) <- round(runif(12,0,1)) M
#> [,1] [,2] [,3] [,4] #> [1,] 1 1 1 0 #> [2,] 1 4 0 1 #> [3,] 1 1 7 1 #> [4,] 0 0 1 10
non.diagonal(M) <- 42 M
#> [,1] [,2] [,3] [,4] #> [1,] 1 42 42 42 #> [2,] 42 4 42 42 #> [3,] 42 42 7 42 #> [4,] 42 42 42 10
non.diagonal(M) <- 1:12 M
#> [,1] [,2] [,3] [,4] #> [1,] 1 4 7 10 #> [2,] 1 4 8 11 #> [3,] 2 5 7 12 #> [4,] 3 6 9 10