I am trying to produce all possible binary matrices order 12x6 containing elements only -1 and +1. There are 2^72 unique matrices (2^16), such that no two matrices contain the same element in the same coordinates.
I tried with the below code:
binary3.2 <- function(m, n) {
mn <- m*n
perm <- RcppAlgos::permuteGeneral(c(-1,1), mn, TRUE)
asplit(array(t(perm), c(m, n, 2^(m*n))), 3)
}
But it's giving me errors mentioning that m*n cannot be greater than 31. In my case, it is 72. Is there any other way to do this?
First off, I am the author of
RcppAlgos. There are a couple of ways around your current issue.Before we start attacking your main issue, let’s start by simplifying your code a bit.
Using
FUNInstead of generating all permutations then calling
asplit, we can make use of theFUNparameter to convert our vector to a matrix on the fly just as we can withcombn:Attacking the Memory Problem
Using
lowerandupperWe can make use of the
lowerandupperparameters so that we can generate permutations in chunks:This gets the job done, however it is somewhat cumbersome.
Is there is a more elegant solution?
A Better Way
RcppAlgosoffers combinatorial iterators so that one can generate n results at a time, thus keeping memory low.Using iterators
The really nice thing about these iterators is that they are bidirectional and they also offer random access:
And as for the question at hand, memory will be no problem!