Zhishi Wang wrote: > > Hi all, > > If I have a vector A<-1:6, I would like to convert A to an upper > matrix M=[0 1 2 3 ; 0 0 4 5 ; 0 0 0 6; 0 0 0 0]. > That is, if the length of the known vector is n, then the order m of > the matrix I'd like to have should satisfy m(m-1)/2=n > How could I do this in R? > I really don't want to do it using a loop. Is there a function in R to do > this ? >
?upper.tri ?uniroot A <- 1:6 n <- length(A) m <- as.integer(uniroot(function(x) {x*(x-1)- 2*n}, c(1, n))$root) U <- matrix(0,nrow=m,ncol=m) U[upper.tri(U)] <- A U Berend -- View this message in context: http://r.789695.n4.nabble.com/how-to-convert-a-vector-to-an-upper-matrix-tp3587715p3587810.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.