I want to generate an nxm matrix. Suppose its 100x3. I want each row to sum to 1 (so two \"0\"\'s and one \"1\").
sample(c(0,0,1),3)
will
Since you want single 1 for a row, the problem can be restated to select a column entry randomly that has 1 for each row.
So you can do like,
m <- 3; n<-100 rand_v <- floor(runif(n)*3)+1 mat <- matrix(0,n,m) idx <- cbind(1:n,rand_v) mat[idx] <- 1
Hope this helps.