Reputation: 21
I am setting up my huge 0-1combination matrix for n arrays in Julia. However, I do not want all the combination as it creates a memory usage problem. I just want to have only a legal combination matching some specific condition and the condition is if column I and column J is 1 then this row should not be in the combination.
I have tried some codes in https://discourse.julialang.org/t/cleanest-way-to-generate-all-combinations-of-n-arrays/20127/6 and then delete the unwanted row, but this failed when it comes to 2^34 combinations.
Let say we have n=6 which result in 64 0-1combinations in total and I want to exclude the combination when the value of element 1 and 4 are 1, and 2 and 5 are 1, and 3 and 6 are 1. The matrix should contain 28 instead of 64 rows like:
0 0 0 0 0 1
0 0 0 0 1 0
0 0 0 0 1 1
0 0 0 1 0 0
0 0 0 1 0 1
0 0 0 1 1 0
0 0 0 1 1 1
0 0 1 0 0 0
0 0 1 0 1 0
0 0 1 1 0 0
0 0 1 1 1 0
0 1 0 0 0 0
0 1 0 0 0 1
0 1 0 1 0 0
0 1 0 1 0 1
0 1 1 0 0 0
0 1 1 0 0 1
0 1 1 1 0 0
1 0 0 0 0 0
1 0 0 0 0 1
1 0 0 0 1 0
1 0 0 0 1 1
1 0 1 0 0 0
1 0 1 0 1 0
1 1 0 0 0 0
1 1 0 0 0 1
1 1 1 0 0 0
0 0 0 0 0 0
Upvotes: 0
Views: 211
Reputation: 8044
Why do you need to materialize that entire array? It's much better to create each combination on the fly when you need it, or create an iterator that gives you the permissible rows one at a time. In the discourse post you link, Stefan describes this too https://discourse.julialang.org/t/cleanest-way-to-generate-all-combinations-of-n-arrays/20127/17 , and as he also says, it's hard to give more advice without knowing what you'll use it for.
You can make an iterator that gives you mostly what you want by
iter = (x for x in Iterators.product(0:1, 0:1, 0:1, 0:1, 0:1, 0:1) if max(x[2] + x[5],x[1] + x[4], x[3] + x[6]) != 2)
You can iterate over iter
in a for
loop or whatever you need it for:
collect(iter)
27-element Array{NTuple{6,Int64},1}:
(0, 0, 0, 0, 0, 0)
(1, 0, 0, 0, 0, 0)
(0, 1, 0, 0, 0, 0)
(1, 1, 0, 0, 0, 0)
(0, 0, 1, 0, 0, 0)
⋮
(0, 0, 0, 0, 1, 1)
(1, 0, 0, 0, 1, 1)
(0, 0, 0, 1, 1, 1)
Upvotes: 3