0

I would like to generate all possible matrix of size NxN such that there are no elements repeated in the rows neither on the columns. Also the matrix values must vary between 1 and N. Is there an optimized way to do it? The purpose of this is that every time I generate one of this matrix, I do a test to it to see if it checks some properties and, if it checks, I don't need to generate more matrixes.

  • You mean a magic square? Check out this post: https://stackoverflow.com/questions/47834140/numpy-equivalent-of-matlabs-magic – warped May 24 '20 at 20:32
  • can an element be repeated in the diagonal or somewhere else? – Nicolas Gervais May 24 '20 at 20:34
  • @NicolasGervais yes, it has to be something like [1, 2, 3, 4], [3, 4, 1, 2], [2, 3, 4, 1], [4, 1, 2, 3] – Diogo Silva May 24 '20 at 20:35
  • @warped i meant something like these [1, 2, 3, 4], [3, 4, 1, 2], [2, 3, 4, 1], [4, 1, 2, 3] – Diogo Silva May 24 '20 at 20:38
  • @warped I checked again the magic square and can I tell to the function with elements can be used to build up the sum I want? For example, in the last mensagem I gave u every row and column would have sum 10 but the elements inside can only be 1,2,3 or 4 – Diogo Silva May 24 '20 at 20:41
  • 2
    Ok, so what you are looking for is a latin square. There are some answers here: https://stackoverflow.com/questions/49606404/efficient-way-of-generating-latin-squares-or-randomly-permute-numbers-in-matrix – warped May 24 '20 at 21:01

0 Answers0