I have an adjacency matrix of the graph like this one:
I want to savethis matrix as it is in a csv or txt file, what should I do?
I have tried the following link
JavaScript
x
2
1
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
2
and others dont give me the right output
Please I need your help
Thank you
Advertisement
Answer
Here you go:
JavaScript
1
8
1
import numpy as np
2
# create random adjacency matrix
3
A = np.random.randint(0,2,(10,10))
4
A[np.arange(10),np.arange(10)] = 1
5
# save to .txt file
6
filename='adjacency_matrix.txt'
7
np.savetxt(filename,A)
8