1

How can I create all binary combinations of matrices with the condition that there can only be a single 1 per column and row. The example will clarify. This particular example must have 6 matrices of combinations, I am showing only the first 2.

c1 <- matrix(c(1, 0, 0, 0, 1, 0, 0, 0, 1), nrow = 3) #First combination
c2 <- matrix(c(0, 1, 0, 1, 0, 0, 0, 0, 1), nrow = 3) #Second combination
Shree
  • 10,835
  • 1
  • 14
  • 36
Ushuaia81
  • 495
  • 1
  • 6
  • 14

1 Answers1

0

What you are asking for is equivalent to finding all permutations of length = n where n = nrow(c1) (or c2 above). Using the FUN argument of permuteGeneral from RcppAlgos (I am the author), we can easily generate the desired outcome:

n <- 3L
myIdentity <- diag(nrow = n)

library(RcppAlgos)
permuteGeneral(n, n, FUN = function(x) myIdentity[x, ])
[[1]]
     [,1] [,2] [,3]
[1,]    1    0    0
[2,]    0    1    0
[3,]    0    0    1

[[2]]
     [,1] [,2] [,3]
[1,]    1    0    0
[2,]    0    0    1
[3,]    0    1    0

[[3]]
     [,1] [,2] [,3]
[1,]    0    1    0
[2,]    1    0    0
[3,]    0    0    1

[[4]]
     [,1] [,2] [,3]
[1,]    0    1    0
[2,]    0    0    1
[3,]    1    0    0

[[5]]
     [,1] [,2] [,3]
[1,]    0    0    1
[2,]    1    0    0
[3,]    0    1    0

[[6]]
     [,1] [,2] [,3]
[1,]    0    0    1
[2,]    0    1    0
[3,]    1    0    0

There are many other ways of generating the requested output. Most notably, utilizing the tried and true combinat package, we can get a similar result (the output will be in a different order):

combinat::permn(3, fun = function(x) myIdentity[x, ])

Now that we have reduced the problem to simply generating permutations, we can use any of the great packages (arrangements, gtools, multicool, partitions, etc.) for generating permutations to obtain our desired result with the help of lapply:

library(arrangements)
myPerms <- permutations(n)
lapply(1:nrow(myPerms), function(x) myIdentity[myPerms[x,], ])
Joseph Wood
  • 7,077
  • 2
  • 30
  • 65
  • your approach works well for matrix of size up to 10. For larger sizes it outputs an error: "The number of rows cannot exceed 2^31 - 1". Do you suggest a solution? It could be made in parts. – Ushuaia81 Nov 21 '18 at 02:59
  • @Alfredo_MF, now to address your comment, yes there are ways for dealing with larger cases. As it is a **[different question](https://meta.stackexchange.com/questions/43478/exit-strategies-for-chameleon-questions "at Stack Exchange sites, “chameleon questions” are not quite welcome")**, I suggest you ask a new question as the approach is completely different. – Joseph Wood Nov 21 '18 at 09:21
  • 1
    Thanks for your comments, I will take in consideration. I saw you are an expert in combinations, if you can give me just a suggestion where to research to solve my problem before making a new question I will appreciate. – Ushuaia81 Nov 21 '18 at 10:47
  • 1
    @Alfredo_MF , I’m glad I can help and I appreciate your willingness to improve. I’m currently traveling but will be able to point you in the right direction later today. – Joseph Wood Nov 21 '18 at 10:55
  • 1
    @Alfredo_MF, now to actually address your real question. Here are some excellent resources for dealing with larger cases (Disclaimer: some of thesel deal with the library that I authored `RcppAlgos`): https://stackoverflow.com/a/51571526/4408538, https://github.com/jwood000/RcppAlgos#using-arguments-lower-and-upper, https://stackoverflow.com/a/22569328/4408538 I will note that `arrangements` is a great library for these types of operations. It is capable of producing iterable objects, so you can generate results one by one at will. – Joseph Wood Nov 22 '18 at 23:43
  • I will look up in the resources. – Ushuaia81 Nov 23 '18 at 15:19