Efficient way of generating latin squares (or randomly permute numbers in matrix uniquely on both axes - using NumPy)

前端 未结 5 1638
悲&欢浪女
悲&欢浪女 2021-01-05 00:31

For example, if there are 5 numbers 1, 2, 3, 4, 5

I want a random result like

[[ 2, 3, 1, 4, 5]
 [ 5, 1, 2, 3, 4]
 [ 3, 2, 4, 5, 1]
 [ 1, 4, 5, 2, 3]         


        
5条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-05 01:22

    Can't type code from the phone, here's the pseudocode:

    1. Create a matrix with one diamention more than tge target matrix(3 d)

    2. Initialize the 25 elements with numbers from 1 to 5

    3. Iterate over the 25 elements.

    4. Choose a random value for the first element from the element list(which contains numbers 1 through 5)

    5. Remove the randomly chosen value from all the elements in its row and column.

    6. Repeat steps 4 and 5 for all the elements.

提交回复
热议问题