Skip to content
Advertisement

How to save adjacency matrix to a file?

I have an adjacency matrix of the graph like this one:

enter image description here

I want to savethis matrix as it is in a csv or txt file, what should I do?

I have tried the following link

https://stackoverflow.com/questions/46462487/how-to-write-an-adjacency-matrix-to-a-file-and-read-from-the-file-you-have-writt Gives me error

and others dont give me the right output

Please I need your help

Thank you

Advertisement

Answer

Here you go:

import numpy as np
# create random adjacency matrix
A = np.random.randint(0,2,(10,10))
A[np.arange(10),np.arange(10)] = 1
# save to .txt file
filename='adjacency_matrix.txt'
np.savetxt(filename,A)
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement