Re-did matrix generation code

I changed the code that generates matrices to, instead of just randomly
sampling from the start, first iterate over all possible matrices with a
single property and cache the ones that are valid w.r.t. the set of
patterns.