Permute rows and columns of a matrix

前端 未结 2 1399
我在风中等你
我在风中等你 2021-01-14 22:05

Assuming that I have the following matrix/array:

array([[0, 0, 1, 1, 1],
       [0, 0, 1, 0, 1],
       [1, 1, 0, 1, 1],
       [1, 0, 1, 0, 0],
       [1, 1         


        
2条回答
  •  暖寄归人
    2021-01-14 22:46

    I found a solution to do what I want (though it is expensive):

    a2 = deepcopy(a1)
    first = randint(0, 5, 10)
    second = randint(0, 5, 10)
    for i in range(len(first)):
        a = deepcopy(a2)
        a2[first[i],:] = a[second[i],:]
        a2[second[i],:] = a[first[i],:]
    for i in range(len(first)):
        a = deepcopy(a2)
        a2[:,first[i]] = a[:,second[i]]
        a2[:,second[i]] = a[:,first[i]] 
    

    Basically, I am doing 10 random switches. However, I need to copy the matrix many times. Anyway, a2 now represents a graph which is isomorphic with a1.

提交回复
热议问题