[R] Help on a matrix task

JeeBee JeeBee at troefpunt.nl
Tue Dec 6 13:41:05 CET 2005


Here is one possible solution:

for(cr in seq(1, dim(combin_mat)[2])) {
  W = which(input_mat[,combin_mat[1,cr]] == 1 & 
            input_mat[,combin_mat[2,cr]] == 1)
  cat("Combination", cr, "(", combin_mat[,cr], ") :", W, "\n")
}

JeeBee.

---
Full program:

N = 4

input_numbers = seq((2^N)-1, 0, -1)
# convert to binary matrix
input_mat = NULL
for(i in seq(N-1,0,-1)) {
  new_col = input_numbers %% 2
  input_mat = cbind(new_col, input_mat)
  input_numbers = (input_numbers - new_col) / 2
}
colnames(input_mat) = NULL

library(gtools)
combin_mat = t(combinations(n=N, r=2, v=1:N, set=TRUE, repeats.allowed=FALSE))

for(cr in seq(1, dim(combin_mat)[2])) {
  W = which(input_mat[,combin_mat[1,cr]] == 1 & 
            input_mat[,combin_mat[2,cr]] == 1)
  cat("Combination", cr, "(", combin_mat[,cr], ") :", W, "\n")
}




More information about the R-help mailing list