I have 5 items each of which can take on the value of 1 or -1. I want to generate a matrix that consists of rows of the possible combinations. The order of the items does not matter and the order of the combinations does not matter. I know I could do this mechanically, but I thought that someone must know a shortcut to generating this matrix. I apologize if this is similar to other questions but none of the solutions I have found can be applied to this particular problem with my programming skills.
Ah, that is so much nicer than mine!
Charles
2010-10-22 03:07:06
Thanks! I love one line of code answers.
ProbablePattern
2010-10-22 13:00:39
A:
Count from 0 to 32 and generate all 5 digit binary numbers. you have all combinations of 0 and 1. when using the combination just substitute 0 with -1 :).
shiva
2010-10-22 03:09:43
+4
A:
To generalize Greg's answer:
N <- 5
vec <- c(-1, 1)
lst <- lapply(numeric(N), function(x) vec)
as.matrix(expand.grid(lst))
caracal
2010-10-22 11:32:00