golra
golra

Reputation: 23

How to delete column and rows in symmetric matrix

I have a symmetric adjacency matrix and I want to reduce it by deleting rows and columns according to their sum. I wrote this function:

def reduce(matrix, min_degree):
    rem = np.where(matrix.sum(axis=0) < min_degree)

    matrix = np.delete(matrix, rem, axis=0)
    matrix = np.delete(matrix, rem, axis=1)

    return matrix

Still by doing this:

adj = reduce(adj, 10)
print(min(adj.sum(axis=0)))

I keep getting values smaller then 10. How can I fix it?

Upvotes: 2

Views: 358

Answers (1)

Gilad Green
Gilad Green

Reputation: 37299

It is not a problem you are getting rows with a sum of less than 10. See the following example: If the adj array is:

array([[1, 1, 1, 1],    # sum 4
       [1, 2, 6, 1],    # sum 10
       [1, 6, 1, 4],    # sum 12
       [1, 1, 4, 5]])   # sum 11

Once you run your function reduce(adj, 10) you get:

array([[2, 6, 1],       # sum 9
       [6, 1, 4],       # sum 10
       [1, 4, 5]])      # sum 10

Which now does have new rows/columns that sum up to less than 10. If you want to continue deleting as long as you still have such rows then you can call the function in a loop while the condition is still met.

Upvotes: 3

Related Questions