venerdì 4 maggio 2007

Fill a matrix with vectors of different lengths

V <- c(1,1,2,3,4,6)
binc <- function(x){
l <- sum(x)+1
y <- c(1,rep(0,l-1))
for (i in x) y <- y+c(rep(0,i),y)[1:l]
}
out <- lapply(1:length(v), function(i) binc(v[1:i]))
nout <- max(sapply(out, length))
sapply(out, function(x) c(x, rep(0, nout - length(x))))

Nessun commento:

Posta un commento