-1

I have problem with my code, i cannot print or change value of Adjacency matrix, can you help me sir? i have code like this

for i in range(len(A.todense())):
    for j in A[i].todense()*1:
        print(j)

and the output is

[[0 0 0 0 0 1 1 0]]
[[0 0 0 0 0 0 0 1]]
[[0 0 0 0 0 1 0 0]]
[[0 0 0 0 0 1 0 0]]
[[0 0 0 0 0 0 0 1]]
[[1 0 1 1 0 0 1 0]]
[[1 0 0 0 0 1 0 0]]
[[0 1 0 0 1 0 0 0]]

and want to change zero to one, or one to zero, but i cannot print or change with A.todense()[i][j]. Can you help me to change the value of adjacency matrix? Thanks you

top up saldo paypal

1 Answers1

0

You can't change value in Adjancency matrix, so that you have to convert to numpy or other array type that can change value.

One Solution Below

A = np.array(A.data)
for i in range(len(A)):
    for j in range(len(A[i])):
        if A[i][j] == 0:
            A[i][j] = 1
        else:
            A[i][j] = 0

That will change values and then convert to Adjancency matrix.

stars seven
  • 132
  • 5
  • 1
    If the array contained bools instead of ints, a simple `~` or `numpy.logical_not()` would suffice. – AMC Mar 02 '20 at 17:34