2D Bit matrix with every possible combination

£可爱£侵袭症+ 提交于 2019-12-12 01:55:44

问题


I need to create a python generator which yields every possible combination of a 2D bit matrix. The length of each dimention is variable.

So for a 2x2 matrix:

1.
00
00

2.
10
00

3.
11
00

....

x.
00
01

Higher lengths of the dimentions (up to 200*1000) need to work too. In the end I will not need all of the combinations. I need only the once where the sum in each line is 1. But I need all combinations where this is the case. I would filter them before yielding. Printing is not required.

I want to use this as filter masks to test all possible variations of a data set. Generating variations like this must be a common problem. Maybe there is even a good library for python?

Robert


回答1:


Going through all possible values of a bit vector of a given size is exactly what a counter does. It's not evident from your question what order you want, but it looks much like a Gray counter. Example:

from sys import stdout

w,h=2,2

for val in range(2**(w+h)):
    gray=val^(val>>1)
    for y in range(h):
        for x in range(w):
            stdout.write('1' if gray & (1<<(w*y+x)) else '0')
        stdout.write('\n')
    stdout.write('\n')

Note that the dimensions of the vector don't matter to the counter, only the size. Also, while this gives every static pattern, it does not cover all possible transitions.




回答2:


This can be done using permutation from itertools in the following way.

import itertools
dim=2
dimension = dim*dim
data = [0 for i in range(0,dimension)] + [1 for i in range(0,dimension)]
count = 1
for matrix in set(itertools.permutations(data,dimension)):
    print('\n',count,'.')
    for i in range(0,dimension,dim):
        print(' '.join(map(str,matrix[i:i+dim])))
    count+=1

P.S: This will be good for 2X2 matrix but a little bit time consuming and memory consuming for higher order. I would be glad if some one provides the less expensive algorithms for this.




回答3:


You can generate every possibility of length 2 by using every number from 0 to 4(2 to the power of 2).

0 -> 00 
1 -> 01 
2 -> 10 
3 -> 11

For the displaying part of a number as binary, bin function can be used.

Since you have 2x2 matrix, you need 2 numbers(i and j), each for a row. Then you can just convert these numbers to binary and print them.

for i in range(4):
    for j in range(4):
        row1 = bin(i)[2:].zfill(2)
        row2 = bin(j)[2:].zfill(2)

        print row1, "\n" , row2, "\n"

EDIT:

I have found zfill function which fills a string with zeros to make it fixed length.

>>> '1'.zfill(5)
'00001'

Another generic solution might be:

import re

dim1 = 2
dim2 = 2
n = dim1 * dim2
i = 0 
limit = 2**n
while i < limit:
    print '\n'.join(re.findall('.'*dim2, bin(i)[2:].zfill(n))), '\n'
    i += 1



回答4:


you could do something like this for 3x3 binary matrix:

for i in range(pow(2,9)):
    p = '{0:09b}'.format(i)
    print(p)
    x = []
    x.append([p[0],p[1],p[2]])
    x.append([p[3],p[4],p[5]])
    x.append([p[6],p[7],p[8]])
    for i in range(3):
        x[i] = map(int, x[i])


来源:https://stackoverflow.com/questions/26777417/2d-bit-matrix-with-every-possible-combination

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!