Skip to content
Back to formatted view

Raw Message

Message-ID: <pan.2005.12.06.12.41.05.55914@troefpunt.nl>
Date: 2005-12-06T12:41:05Z
From: JeeBee
Subject: Help on a matrix task

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")
}