[R] how to convert a vector to an upper matrix

Berend Hasselman bhh at xs4all.nl
Sun Jun 17 10:22:31 CEST 2012


ozzi wrote
> 
> Dear Berend,
> 
> the coding is not working for more than 6. Try to plug 10 or other number.
> Would you please correct ?
> 

My mistake.

mkupper <- function(A) {
    n <- length(A)
    m <- (1+sqrt(1+8*n))/2
    stopifnot(m==as.integer(m))
    print(m)
    L <- matrix(0,nrow=m,ncol=m)
    L[lower.tri(L)] <- A

    U <- t(L)
    U
}

A <- 1:6
mkupper(A)


--
View this message in context: http://r.789695.n4.nabble.com/how-to-convert-a-vector-to-an-upper-matrix-tp3587715p4633630.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list