vec2mat1=function(vec){
l=length(vec)
p=round(uniroot(function(x)x*(x+1)/2-l,c(0,1))[[1]],0)
a=diag(vec[1:p],p)
a[lower.tri(a)]=tail(vec,-p)
a[upper.tri(a)]=t(a)[upper.tri(a)]
a
}
vec2mat1(1:6)
[,1] [,2] [,3]
[1,] 1 4 5
[2,] 4 2 6
[3,] 5 6 3
vec2mat1(1:10)
[,1] [,2] [,3] [,4]
[1,] 1 5 6 7
[2,] 5 2 8 9
[3,] 6 8 3 10
[4,] 7 9 10 4