Takes a vector and, if the vector is of the correct lenght to be made into a symmetric matrix, performs the conversion.

vec2symmat(invec, diag = 1, byrow = TRUE)

Arguments

invec

The input vector

diag

The value for the diagonal

byrow

Logical. Whether the upper-triangle should be filled in by row

Value

A matrix

References

http://stackoverflow.com/a/18598933/1270695

Author

Ananda Mahto

Examples

myvec <- c(-.55, -.48, .66, .47, -.38, -.46) vec2symmat(myvec)
#> [,1] [,2] [,3] [,4] #> [1,] 1.00 -0.55 -0.48 0.66 #> [2,] -0.55 1.00 0.47 -0.38 #> [3,] -0.48 0.47 1.00 -0.46 #> [4,] 0.66 -0.38 -0.46 1.00
vec2symmat(1:15, diag = 0)
#> [,1] [,2] [,3] [,4] [,5] [,6] #> [1,] 0 1 2 3 4 5 #> [2,] 1 0 6 7 8 9 #> [3,] 2 6 0 10 11 12 #> [4,] 3 7 10 0 13 14 #> [5,] 4 8 11 13 0 15 #> [6,] 5 9 12 14 15 0
vec2symmat(1:15, diag = 0, byrow = FALSE)
#> [,1] [,2] [,3] [,4] [,5] [,6] #> [1,] 0 1 2 4 7 11 #> [2,] 1 0 3 5 8 12 #> [3,] 2 3 0 6 9 13 #> [4,] 4 5 6 0 10 14 #> [5,] 7 8 9 10 0 15 #> [6,] 11 12 13 14 15 0