how to generate all possible combinations of a 14x10 matrix containing only 1's and 0's

前端 未结 11 1006
悲&欢浪女
悲&欢浪女 2021-01-23 23:32

I\'m working on a problem and one solution would require an input of every 14x10 matrix that is possible to be made up of 1\'s and 0\'s... how can I generate these so that I can

相关标签:
11条回答
  • 2021-01-24 00:15

    Depending on what you want to accomplish with the generated matrices, you might be better off generating a random sample and running a number of simulations. Something like:

    matrix_samples = []
    # generate 10 matrices
    for i in range(10):
        sample = numpy.random.binomial(1, .5, 14*10)
        sample.shape = (14, 10)
        matrix_samples.append(sample)
    

    You could do this a number of times to see how results vary across simulations. Of course, you could also modify the code to ensure that there are no repeats in a sample set, again depending on what you're trying to accomplish.

    0 讨论(0)
  • 2021-01-24 00:16

    Are you sure you want every possible 14x10 matrix? There are 140 elements in each matrix, and each element can be on or off. Therefore there are 2^140 possible matrices. I suggest you reconsider what you really want.

    Edit: I noticed you mentioned in a comment that you are trying to minimize something. There is an entire mathematical field called optimization devoted to doing this type of thing. The reason this field exists is because quite often it is not possible to exhaustively examine every solution in anything resembling a reasonable amount of time.

    0 讨论(0)
  • 2021-01-24 00:18

    Trying this:

    import numpy
    for i in xrange(int(1e9)): a = numpy.random.random_integers(0,1,(14,10))
    

    (which is much, much, much smaller than what you require) should be enough to convince you that this is not feasible. It also shows you how to calculate one, or few, such random matrices even up to a million is pretty fast).

    EDIT: changed to xrange to "improve speed and memory requirements" :)

    0 讨论(0)
  • 2021-01-24 00:19

    Are you saying that you have a table with 140 cells and each value can be 1 or 0 and you'd like to generate every possible output? If so, you would have 2^140 possible combinations...which is quite a large number.

    0 讨论(0)
  • 2021-01-24 00:19

    Instead of just suggesting the this is unfeasible, I would suggest considering a scheme that samples the important subset of all possible combinations instead of applying a brute force approach. As one of your replies suggested, you are doing minimization. There are numerical techniques to do this such as simulated annealing, monte carlo sampling as well as traditional minimization algorithms. You might want to look into whether one is appropriate in your case.

    0 讨论(0)
提交回复
热议问题