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

前端 未结 11 1005
悲&欢浪女
悲&欢浪女 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-23 23:59

    You don't have to iterate over this:

    def everyPossibleMatrix(x,y):
        N=x*y
        for i in range(2**N):
            b="{:0{}b}".format(i,N)
            yield '\n'.join(b[j*x:(j+1)*x] for j in range(y))
    
    0 讨论(0)
  • 2021-01-24 00:02

    Generating Every possible matrix of 1's and 0's for 14*10 would generate 2**140 matrixes. I don't believe you would have enough lifetime for this. I don't know, if the sun would still shine before you finish that. This is why it is impossible to generate all those matrices. You must look for some other solution, this looks like a brute force.

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

    This is absolutely impossible! The number of possible matrices is 2140, which is around 1.4e42. However, consider the following...

    • If you were to generate two 14-by-10 matrices at random, the odds that they would be the same are 1 in 1.4e42.
    • If you were to generate 1 billion unique 14-by-10 matrices, then the odds that the next one you generate would be the same as one of those would still be exceedingly slim: 1 in 1.4e33.
    • The default random number stream in MATLAB uses a Mersenne twister algorithm that has a period of 219936-1. Therefore, the random number generator shouldn't start repeating itself any time this eon.

    Your approach should be thus:

    • Find a computer no one ever wants to use again.
    • Give it as much storage space as possible to save your results.
    • Install MATLAB on it and fire it up.
    • Start computing matrices at random like so:

      while true
        newMatrix = randi([0 1],14,10);
        %# Process the matrix and output your results to disk
      end
      
    • Walk away

    Since there are so many combinations, you don't have to compare newMatrix with any of the previous matrices since the length of time before a repeat is likely to occur is astronomically large. Your processing is more likely to stop due to other reasons first, such as (in order of likely occurrence):

    • You run out of disk space to store your results.
    • There's a power outage.
    • Your computer suffers a fatal hardware failure.
    • You pass away.
    • The Earth passes away.
    • The Universe dies a slow heat death.

    NOTE: Although I injected some humor into the above answer, I think I have illustrated one useful alternative. If you simply want to sample a small subset of the possible combinations (where even 1 billion could be considered "small" due to the sheer number of combinations) then you don't have to go through the extra time- and memory-consuming steps of saving all of the matrices you've already processed and comparing new ones to it to make sure you aren't repeating matrices. Since the odds of repeating a combination are so low, you could safely do this:

    for iLoop = 1:whateverBigNumberYouWant
      newMatrix = randi([0 1],14,10);  %# Generate a new matrix
      %# Process the matrix and save your results
    end
    
    0 讨论(0)
  • 2021-01-24 00:06

    I was actually much more pessimistic to begin with, but consider:

    from math import log, e
    
    def timeInYears(totalOpsNeeded=2**140, currentOpsPerSecond=10**9, doublingPeriodInYears=1.5):
        secondsPerYear = 365.25 * 24 * 60 * 60
        doublingPeriodInSeconds = doublingPeriodInYears * secondsPerYear
        k = log(2,e) / doublingPeriodInSeconds  # time-proportionality constant
        timeInSeconds = log(1 + k*totalOpsNeeded/currentOpsPerSecond, e) / k
        return timeInSeconds / secondsPerYear
    

    if we assume that computer processing power continues to double every 18 months, and you can currently do a billion combinations per second (optimistic, but for sake of argument) and you start today, your calculation will be complete on or about April 29th 2137.

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

    Here is an efficient way to do get started Matlab:

    First generate all 1024 possible rows of length 10 containing only zeros and ones:

    dec2bin(0:2^10-1)
    

    Now you have all possible rows, and you can sample from them as you wish. For example by calling the following line a few times:

    randperm(1024,14)
    
    0 讨论(0)
  • 2021-01-24 00:15

    The actual implementation depends heavily on how you want to represent matrices… But assuming the matrix can be represented by a 14 * 10 = 140 element list:

    from itertools import product
    for matrix in product([0, 1], repeat=140):
        # ... do stuff with the matrix ...
    

    Of course, as other posters have noted, this probably isn't what you want to do… But if it really is what you want to do, that's the best code (given your requirements) to do it.

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