Dear R-users,

would it be a better way to construct the matrix below without using any 
for-loop or model.matrix? preferably with some matrix algebra?

Thanks in advance,
Carlo Giovanni Camarda

## dimensions
m <- 3
n <- 4
mn <- m*n 
k <- m+n-1

## with a for-loop
X <- matrix(0, mn, k)
for(i in 1:n){
  wr <- 1:m+(i-1)*m
  wc <- rev(1:m+(i-1))
  where <- cbind(wr,wc)
  X[where] <- 1
}

## using model.matrix
ff <- factor(c(outer(m:1, seq(0,m), "+")))
X1 <- matrix(model.matrix(~-1+ff), mn, k)



----------
This mail has been sent through the MPI for Demographic ...{{dropped:10}}

______________________________________________
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.

Reply via email to